【问题标题】:Is there a way to loop a switch case base program from within the switch case statement?有没有办法从 switch case 语句中循环 switch case 基础程序?
【发布时间】:2015-08-13 18:06:41
【问题描述】:

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

  • 我尝试使用 continue,除非 switch-case 块在循环内,否则它不起作用。
  • 我尝试设计一个带条件的 while 循环,但效果并不准确,因为它不会执行 Cases 内的块,并且会简单地跳出 switch 并成功终止程序而不执行 case。
  • 我尝试了 goto,但似乎对要使用的标签有一定的规则,目前不赞成在 Switch 中使用 goto。

我的研究仅显示人们在外部循环 Switch-Case(例如在 while 循环或 for 循环中的 Switch-Case 等),但找不到任何人试图从 Switch-Case 块内构建循环的示例的代码。 我不介意解决方案是从头开始重复代码还是再次从 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 内的块,但它刚刚爆发了。

输出图片:

【问题讨论】:

  • 您的代码并没有真正检查正确的输入,尝试输入asdasd 并且会出现未定义的行为。另外,你为什么要那样做?
  • 是的,我知道我尝试输入了一个字母,是的,它进入了无限期打印。但这不是重点.. 这是我可以重新设计和纠正的东西,只有在我首先弄清楚我正在寻找的这个基本东西是否可能之后。无论如何感谢您的说明。顺便说一句,我的问题是错误的,它立即收到了负面标记
  • 不是很清楚,而且你要求的东西没人会尝试做,所以我很好奇你为什么这样做?
  • 您的问题被否决了,因为它充其量很难理解您要做什么。根本不清楚你的意思。您的示例代码中没有任何循环。 switch 语句不打算成为循环;试图滥用它使其像循环一样工作是不明智的。但是你应该停止专注于你正在使用的技术并解释你想要做什么。从表面上看,一个普通的while 循环在条件中调用一个输入函数并对返回值做出适当的反应,就可以满足您的需要。
  • @我很担心,因为 stackoverflow 是一个很好的澄清资源,我不希望我的帐户被禁止或禁止提问

标签: c loops while-loop switch-statement


【解决方案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 正确(使用适当的块来避免跳过构造函数调用的错误)或将外部循环拉到一个单独的函数中,然后return out当你想结束时(这也适用于main)。

不过,您在示例中所做的完全没有。那里根本没有循环。

【讨论】:

  • 是的,谢谢,在我粘贴的代码中我什么也没做,但在我的试验中我尝试了一个 while 循环,但我没有重复整个 switch-case 代码。你的回答很好,但这是我所说的另一种情况。但我确实意识到我相信我已经发现自己无法跳出结构。
  • 我不知道你在说什么其他情况。顺便说一句,这就是我对你的问题投反对票的原因,它写得非常糟糕。
  • 到底写得不好,是英语不好还是粗体突出还是不合逻辑还是我应该撤回我自己的问题???
  • 好吧,不要加粗随机词(“C 编程”是无用的噪音,问题已经被标记),尝试加粗你的实际问题,措辞尽可能清晰。然后删除其他所有内容,因为它也是噪音。然后删除所有粗体,因为剩下的都很重要。
【解决方案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;
}

【讨论】:

  • 是的!!!你在你的回答中显示的第一个案例是我最初尝试的,是的,这是被混淆的,因为它没有正确执行代码,我也尝试过 continue 和 goto 以及类似的情况,但只是发现它不适合执行案例并简单地打破返回零的整个代码。但终于有人理解了我正在考虑的路线,是的,现在我明白现在实际上有办法做我想做的事(至少没有正式的方式)。非常感谢弗拉德。
  • “最好将 switch 语句包含在 do while 语句中。例如”,这对我来说不是问题,我知道这是正确的方法,只是我正在寻找因为我是学习者而不是专业人士,所以寻找可能性
  • 是的,我今天学到的不仅仅是一些东西,我非常感谢您为我的查询链线带来您的体贴,以提供解决方案。正如您所看到的最终结果,最后 some1 实际上有点精确地指出了实际问题,并在同一行查询中发布了一些更接近的解决方案。非常感谢。查看乔纳森的帖子。它有点朝向条件迭代,令人震惊,但不是太令人震惊,而是有趣且近乎完美。
【解决方案3】:

如果你想要乱七八糟的代码,你可以在这个代码上乱用变体:

#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 — 除非您将break 替换为continue,或某种goto,或return)。

免责声明

这是令人震惊的代码——不要使用它!

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

【讨论】:

  • 嘿,非常感谢您的努力,我想我将尝试使用这个 Goto 重做版本并检查它是否有效。 p,s,我知道定义函数不包含在我的原始问题帖子中,所以我在此代码之前将代码更改为我的初稿,我试图完成工作
  • 嘿,我试过了,它工作得非常好,可以回答我的问题现在我明白这种“不太疯狂”的方法并不是很有用,但它只是没有作为学习者了解某些特征的局限性是有害的。哇终于在 14 小时后恢复了我的轨道,是的,这是一个完美的网格,可以用 Switch case 语句来试验我的意图。这与我想尝试的目标非常接近并且几乎是正确的。一百万谢谢。我在这件事上花了 14 个小时,现在每一秒都值得。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-29
  • 1970-01-01
  • 2021-05-23
  • 2014-03-27
  • 1970-01-01
相关资源
最近更新 更多