如何循环遍历在 C++ 中再次要求输入的 switch-case 语句?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15338209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:17:25  来源:igfitidea点击:

How do I loop through a switch-case statement that asks for input again in c++?

c++while-loopswitch-statement

提问by John

First I am displaying a menu to the user in which they can make a choice from. Once they make a choice, some code is executed and I want to show them the same menu again so they can make a different choice.

首先,我向用户显示一个菜单,他们可以从中进行选择。一旦他们做出选择,就会执行一些代码,我想再次向他们显示相同的菜单,以便他们做出不同的选择。

This is my code so far:

到目前为止,这是我的代码:

//call the menu function and put it in a variable which will be used in the switch case statements
menu_selection = main_menu();

condition_main = true;

while (condition_main) {    
    switch (menu_selection) {
        case 1: display(primes);
                menu_selection = main_menu();

        case 2: display(fibos);
                menu_selection = main_menu();

        case 3: display(primes_and_fibos);
                menu_selection = main_menu();

        case 4: display(primes_not_fibos);
                 menu_selection = main_menu();

        case 5: display(fibos_not_primes);
                 menu_selection = main_menu();

        case 6: search();
                 menu_selection = main_menu();

        case 7: condition_main = false;
                             return 0; //this exits the program
        default: cout << "\nThat is an invalid option. Please try again.\n\n";

    } 
}

When I run the program however, what seems to be happening is that if I choose option 1, it would do the code and the menu is shown again but if I choose option 1 again it would run the code designed for option 2. I have also tried a do while loop and not using a loop at all and nothing seems to work. Any help is appreciated, thanks.

然而,当我运行程序时,似乎发生的是,如果我选择选项 1,它将执行代码并再次显示菜单,但如果我再次选择选项 1,它将运行为选项 2 设计的代码。我有还尝试了 do while 循环并且根本不使用循环,但似乎没有任何效果。任何帮助表示赞赏,谢谢。

采纳答案by Shafik Yaghmour

Each case needs a break unless you desire to have one condition fall through to the next:

每个案例都需要休息,除非您希望让一个条件落入下一个条件:

switch (menu_selection) {
    case 1: display(primes);
            menu_selection = main_menu();
            break ;

    case 2: display(fibos);
            menu_selection = main_menu();
            break ;

    case 3: display(primes_and_fibos);
            menu_selection = main_menu();
            break ;

    case 4: display(primes_not_fibos);
             menu_selection = main_menu();
            break ;

    case 5: display(fibos_not_primes);
             menu_selection = main_menu();
            break ;

    case 6: search();
             menu_selection = main_menu();
            break ;

    case 7: condition_main = false;
                         return 0; //this exits the program
    default: cout << "\nThat is an invalid option. Please try again.\n\n";
            break ;
} 

this referencehas more switchexamples and also covers the falling through case as well.

这个参考有更多的switch例子,也涵盖了跌倒的情况。

As Steve suggested it also probably makes more sense to have the returnafter the whilestatement. This is would be the expected control flow and will most likely be easier to maintain as well. Moving the main_menucall out of switch into the loop will also make your code easier to change later on.

正如史蒂夫所建议的那样return,在while声明之后使用也可能更有意义。这将是预期的控制流,并且很可能也更容易维护。将main_menu调用从 switch 移到循环中也会使您的代码以后更容易更改。

回答by Steve Valliere

I agree, the break is missing. You might want to consider a code structure more like this: (my formatting isn't great because I was going for compact)

我同意,休息时间不见了。您可能需要考虑更像这样的代码结构:(我的格式不是很好,因为我想要紧凑)

do {
   menu_selection = main_menu();
   switch (menu_selection) {
   case 1 : display(primes);            break;
   case 2 : display(fibos);             break;
   case 3 : display(primes_and_fibos);  break;
   case 4 : display(primes_not_fibos);  break;
   case 5 : display(fibos_not_primes);  break;
   case 6 : search();                   break;
   case 7 : break;
   default: cout << "\nThat is an invalid option. Please try again.\n\n";
} while (condition_main != 7);
return 0;

(formatted the way you prefer, of course.) This has only a single call to the menu function, making maintenance much easier. The returnis also at the end of the function, where most people expect to find it.

(当然,按照您喜欢的方式格式化。)这只需调用一次菜单功能,使维护更加容易。将return在功能,大多数人希望找到它的到底是也。

回答by Peter Wood

Adding breaks will 'fix' the problem.

添加breaks 将“修复”问题。

However, it would be better to separate out the code to make it easier to test. You might have realised the problem yourself then.

但是,最好将代码分开以使其更易于测试。那时您可能已经意识到了这个问题。

Separate your input and your handling code:

将输入和处理代码分开:

bool handleSelection(int menu_selection) {
    switch (menu_selection) {
        case 1: display(primes);
        case 2: display(fibos);
        case 3: display(primes_and_fibos);
        case 4: display(primes_not_fibos);
        case 5: display(fibos_not_primes);
        case 6: search();
        case 7: return false;
        default: cout << "\nThat is an invalid option. Please try again.\n\n";
    }
    return true;
}

void pump() {
    int menu_selection = main_menu();
    while(handleSelection(menu_selection)) {    
        menu_selection = main_menu();
    }
}

int main() {
    //pump();
    handleSelection(3);    
}

You should probably separate the output code, too ("That is an invalid option. Please try again"), but I think I've made my point without doing that.

您也应该将输出代码分开(“这是一个无效选项。请再试一次”),但我想我没有这样做就已经说明了我的观点。

回答by Pankaj

boolean condition = true;
while(condition==true){
System.out.println("Press Number \n 1 for check prime \n 2 for exit");
Scanner scannerOption = new Scanner(System.in);
    int option = scannerOption.nextInt();

    switch (option){
        case 1:
            checkPrime();
            break;
        case 2:
            condition = false;
            break;
        default:
            System.out.println("Wrong Enter plz try again");

    }
}