有没有办法从switch case语句中循环switch case基程序?

有没有办法从switch case语句中循环switch case基程序?,c,loops,while-loop,switch-statement,C,Loops,While Loop,Switch Statement,因此,当我练习切换案例块的变体时,我想尝试循环程序中的切换案例部分,而在我的试验中,我目前无法从中循环。我尝试使用continue,除非switch case块在循环中,否则它不会工作。我试着设计一个带条件的while循环,虽然有效,但并不准确,因为它不会执行案例中的块,但它会简单地跳出开关并成功终止程序,但案例没有执行。我尝试了goto,但我相信对于标签的使用有一定的规则,到目前为止,这不利于在交换机内部使用goto。 我的研究只显示人们在外部循环开关用例(如while循环或for循环中的开关

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

这是我的密码:-

    #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;
    }
#包括
int main()
{
int输入;
printf(“4.Play game\n”);//我已重新编辑了问题,缩短了代码,并包含了我根据建议所做的额外尝试,以便更多地关注问题,而不是更改主要问题。
printf(“选择:”);
scanf(“%d”,输入(&I));
开关(输入)
{
案例4:
printf(“感谢播放!Taaaa\n”);
打破
违约:
while(input!=4)//只是一个示例条件,我知道它不会检查输入的字母或字符,但这不是重点。我只是想看看是否存在类似思想/方法的解决方案。
{   
printf(“不,您必须提供正确的输入\n”);
scanf(“%d”,输入(&I));
continue;//我也在这个循环中尝试了一个Goto和return;结果发现它不会让我跳出这个循环并返回到程序中。
}
}
返回0;
}
或者,我可以对我使用的while循环做些什么呢?while循环使它运行起来,但还没有准确无误或警告。唯一的问题是,正如我前面所描述的,while循环并没有在cases中执行块,但它只是爆发了


我最初尝试附加输出图像,但由于向下投票,我的声誉似乎低于10,因此无法附加抱歉。现在再次连接。

做这类事情的传统方法是使用简单的
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中,您可以标记外部循环并执行类似于
breakouterloop,但据我所知,这是唯一实现它的类C语言

因此,在其他语言中,您有两种选择:正确地使用
goto
(使用适当的块以避免跳过构造函数调用的错误),或者在单独的函数中拉出外部循环,并在想要结束时从中返回
(这也适用于
main


不过,你在例子中所做的完全没有什么。这里根本没有循环。

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

#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;
}
#包括
内部主(空)
{
int输入;
printf(“1.玩游戏\n”);
printf(“2.设计游戏”);
printf(“3.Play multiplayer\n”);
printf(“4.退出\n”);
printf(“选择:”);
scanf(“%d”,输入(&I));
开关(输入)
{
做
{
案例1:/*请注意冒号,而不是分号*/
puts(“playgame()”);
打破
案例2:
看跌期权(“Designgame()”);
打破
案例3:
puts(“playmultiplayer()”);
打破
案例4:
printf(“Taaaa!\n”);
打破
违约:
printf(“不,您必须提供正确的输入\n”);
printf(“选择:”);
scanf(“%d”,输入(&I));
}而(1),;
}
返回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;
} 
#包括
内部主(空)
{
int输入;
printf(“1.玩游戏\n”);
printf(“2.设计游戏”);
printf(“3.Play multiplayer\n”);
printf(“4.退出\n”);
printf(“选择:”);
scanf(“%d”,输入(&I));
开关(输入)
{
做
{
如果(输入=1)
{
案例1:/*请注意冒号,而不是分号*/
puts(“playgame()”);
打破
}
else if(输入=2)
{
案例2:
看跌期权(“Designgame()”);
打破
}
else if(输入=3)
{
案例3:
puts(“playmultiplayer()”);
打破
}
else if(输入=4)
{
案例4:
printf(“Taaaa!\n”);
打破
}
其他的
{
违约:
printf(“不,您必须提供正确的输入\n”);
scanf(“%d”,输入(&I));
}
}而(1),;
}
返回0;
} 
虽然这是一个有效的代码,但我
#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;
}
#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__); }