有没有办法从switch case语句中循环开关案例库程序?

时间:2015-05-31 04:36:06

标签: c loops while-loop switch-statement

因此,当我练习switch-case块的变体时,我想尝试循环程序的Switch-Case部分,在我的试验中我无法做到这一点。我尝试使用continue,除非switch-case块在循环内,否则它不起作用。我尝试设计一个带有条件的while循环,虽然有效但不准确,因为它没有执行Case中的块,但它只是突破了switch并成功终止程序但是没有执行。我试过goto,但我相信对于使用的标签有一定的规则,到目前为止还不支持在Switch内部使用goto。 我的研究只显示人们在外部循环Switch-Case(比如一个while-loop循环中的Switch-Case或for循环等),但找不到任何试图从Switch-Case代码块中构建循环的例子。 我不介意解决方案是从开始重复代码还是从Switch-Case外部的外部点开始重复,但目的是让程序以某种方式重新执行Switch-Case而不包含相同的Switch-案例循环。 经过很长一段时间后我再次学习C编程,因此我真的对此进行了长时间的搜索,但找不到合适的结果。也许在很长一段时间后我也会进行长时间的搜索,所以我可能会失去联系。 有一个类似的问题,但用户给出和接受的解决方案表明使用do while封装开关盒而不是我想要的另一种方式。这是一个链接

C programming do while with switch case program

这是我的代码: -

    #include <stdio.h>
    int main()
    {
    int input;

    printf( "4. Play game\n" );      //i have re-edited the question and shortened the code and included what I additionally tried as per the advice, to focus on the question more, w/o changing the main question .
    printf( "Selection: " );
    scanf( "%d", &input );
    switch ( input ) 
    {
        case 4:
            printf( "Thanks for playing! Taaaa\n" );
            break;
        default: 
            while(input!=4)          //just a sample condition, and i know that it doesnt check for letters or characters as input, but that is not the point. I just want to see if a solution on similar thought/methodology exists.
           {   
            printf( "no u have to give a correct input\n" );
            scanf("%d", &input);
            continue;                // I tried a Goto and return; in this loop as well only to realize that it will not jump me out of this loop and back into the program.
           }
    }
    return 0;
    }

或者,我可以对我使用的while循环做什么,使其运行但不准确但没有错误或警告。唯一的问题是while循环,正如我前面所描述的那样,并没有在case中执行块,但它刚刚爆发。

我最初尝试附加输出图像,但由于投票失败,我的声誉似乎低于10,因此无法附上抱歉。现在再次附上。enter image description here

3 个答案:

答案 0 :(得分:2)

如果我理解正确,那么你的意思是以下

#include <stdio.h>

int main( void )
{
    int input;

    printf( "1. Play game\n" );
    printf( "2. Design game\n" );
    printf( "3. Play multiplayer\n" );
    printf( "4. Exit\n" );
    printf( "Selection: " );
    scanf( "%d", &input );

    switch ( input ) 
    {
    do
    {
    case 1:            /* Note the colon, not a semicolon */
        puts( "playgame()" );
        break;
    case 2:          
        puts( "Designgame()" );
         break;
    case 3:         
        puts( "playmultiplayer()" );
        break;
    case 4:        
        printf( "Taaaa!\n" );
        break;
    default:            
        printf( "no u have to give a correct input\n" );
        printf( "Selection: " );
        scanf( "%d", &input );
    } while ( 1 );
    }

    return 0;
}

看起来更精确的代码

#include <stdio.h>

int main( void )
{
    int input;

    printf( "1. Play game\n" );
    printf( "2. Design game\n" );
    printf( "3. Play multiplayer\n" );
    printf( "4. Exit\n" );
    printf( "Selection: " );
    scanf( "%d", &input );

    switch ( input ) 
    {
        do
        {
            if ( input == 1 )
            {
                case 1:            /* Note the colon, not a semicolon */
                puts( "playgame()" );
                break;
            }
            else if ( input == 2 )
            {
                case 2:          
                    puts( "Designgame()" );
                    break;
            }
            else if ( input == 3 )
            {
                case 3:         
                puts( "playmultiplayer()" );
                break;
            }
            else if ( input == 4 )
            {
                case 4:        
                printf( "Taaaa!\n" );
                break;
            }
            else
            {
                default:            
                printf( "no u have to give a correct input\n" );
                scanf( "%d", &input );
            }
        } while ( 1 );
    }

    return 0;
} 

虽然它是有效的代码,但是它被混淆且不可读。

将switch语句括在do while语句中要好得多。例如

#include <stdio.h>

int main( void )
{
    int input;

    do
    {

        printf( "1. Play game\n" );
        printf( "2. Design game\n" );
        printf( "3. Play multiplayer\n" );
        printf( "4. Exit\n" );
        printf( "Selection: " );
        scanf( "%d", &input );

        switch ( input ) 
        {
        case 1:            /* Note the colon, not a semicolon */
            puts( "playgame()" );
            break;
        case 2:          
            puts( "Designgame()" );
            break;
        case 3:         
            puts( "playmultiplayer()" );
            break;
        case 4:        
            printf( "Taaaa!\n" );
            break;
        default:            
            printf( "no u have to give a correct input\n" );
            break;
        }
    }  while ( input < 1 || input > 4 );

    return 0;
}

答案 1 :(得分:2)

如果您想要疯狂的代码,可以使用此代码中的变体:

#include <stdio.h>

/*
** DISCLAIMER
** This is an appalling idea - do not use it.
** But it seems to meet the criteria of SO 30553881.
** Maybe.
*/

extern void PlayGame(void);
extern void DesignGame(void);
extern void PlayMultiplayer(void);

int main(void)
{
    int input;

    printf("1. Play game\n");
    printf("2. Design game\n");
    printf("3. Play multiplayer\n");
    printf("4. Exit\n");
    printf("5. Auto-try-again\n");
    printf("Selection: ");
    if (scanf("%d", &input) == 1)
    {
redo:
        switch (input)
        {
            while (input > 0 && input != 4)
            {
            case 1:
                PlayGame();
                break;
            case 2:
                DesignGame();
                break;
            case 3:
                PlayMultiplayer();
                break;
            case 4:
                printf("Taaaa!\n");
                break;
            default:
                printf("no u have to give a correct input\n");
                break;
            }
            if (input != 4 && scanf("%d", &input) == 1)
                goto redo;
        }
    }
    return 0;
}

void PlayGame(void)        { printf("%s called\n", __func__); }
void DesignGame(void)      { printf("%s called\n", __func__); }
void PlayMultiplayer(void) { printf("%s called\n", __func__); }

请注意,案例标签后的break;语句会导致while循环而非switch。您可以使用continue代替break,但这意味着下一次迭代将执行PlayGame()然后退出循环(因为break - 除非您替换break continue,或某种gotoreturn

声明

这是令人震惊的代码 - 请勿使用它!

但它确实表明你可以在C中做各种真正疯狂的事情。您可以查找Duff's Device以查看与此有些相关的合理有用的代码块(这不太合理)。

答案 2 :(得分:1)

执行此类操作的传统方法是使用简单的do..while构造:

bool ending;
do
{
   // print menu
   // read input in variable
   // switch on your input variable, set ending to true if supposed to end
}while(!ending);

你发现自己无法跳出嵌套的循环结构。例如,在Java中,您可以标记外部循环并执行类似break outerloop;的操作,但据我所知,这是实现它的唯一类C语言。

所以在其他语言中你有两个选择:正确使用goto (使用适当的块来避免跳过构造函数调用的错误)或者在单独的函数中拉出外部循环并{{ 1}}当你想要结束时(也适用于return)。

你在你的例子中所做的一切都没有。那里根本没有循环。

相关问题