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

Is there a way to loop a switch case base program from within the switch case statement?

因此,当我练习 switch-case 块的变体时,我想尝试循环程序的 Switch-Case 部分,到目前为止我在试验中无法从中循环。

我的研究仅显示人们在外部循环 Switch-Case(例如 while 循环或 for 循环中的 Switch-Case 等),但找不到任何人试图从 Switch 内部构建循环的示例-案例代码块。 我不介意解决方案是从头开始重复代码还是再次从 Switch-Case 外部的外部点开始,但目的是让程序以某种方式重新执行 Switch-Case 而无需将相同的 Switch-Case 封装在一个循环。 很长一段时间后,我再次学习 C 编程,因此我确实为此做了很长时间的搜索,但找不到合适的结果。也许我也在很长一段时间后进行了长时间的搜索,所以我可能已经失去了联系。 有一个类似的问题,但用户给出并接受的解决方案表明与我在这里要求的不同。题 : 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 内的块,但它刚刚爆发。

输出图像:

做这种事情的传统方法是使用一个简单的 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 适当(使用适当的块来避免关于跳过构造函数调用的错误)或者将外循环拉入一个当你想结束时,将函数和 return 分开(这也适用于 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;
}

更准确的代码看起来

#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;
}

如果你想要疯狂的代码,你可以弄乱这段代码的变体:

#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__); }

请注意,case 标签后的 break; 语句打破了 while 循环,而不是 switch。您可以使用 continue 而不是 break,但这意味着下一次迭代将执行 PlayGame() 然后退出循环(因为 break - 除非您替换它breakcontinue,或某种 goto,或 return)。

免责声明

这是可怕的代码——不要使用它!

但它确实表明您可以用 C 语言做各种非常疯狂的事情。您可以查找 Duff's Device 以查看与此有点相关的看似有用的代码块(这似乎不太有用)。