简体   繁体   English

为什么我只能在我的代码中使用一个“while”循环?

[英]Why can I use only one "while" loop in my code?

I am writing a program in C++, and I want the user to make a choice between a few options, and each option is different.我正在用 C++ 编写一个程序,我希望用户在几个选项之间做出选择,每个选项都不同。 At the end of the option, I want the user to be able to select a different option from a menu, but if the user originally chooses option 3, when the user gets back to the menu, if he/she chooses 1 or 2 it terminates the program.在选项的最后,我希望用户能够从菜单中选择不同的选项,但是如果用户最初选择选项 3,当用户返回菜单时,如果他/她选择 1 或 2 它终止程序。 What can I do to make the code repeat itself?我该怎么做才能使代码重复?

#include <iostream>
using namespace std;

int main() {
   int play;
   cout << "What do you want to do now?" << endl;
   cout << "Choose a number..." << endl;
   cout << "1) Talk." << endl;
   cout << "2) Vent." << endl;
   cout << "3) Play a guessing game." << endl;
   cout << "4) End." << endl;
   cin >> play;

   while (play == 1){
      //code here
      cout << "What do you want to do now?" << endl;
      cout << "Choose a number..." << endl;
      cout << "1) Talk." << endl;
      cout << "2) Vent." << endl;
      cout << "3) Play a guessing game." << endl;
      cout << "4) End." << endl;
      cin >> play;
   }

   while (play == 2){
      //code goes here
      cout << "What do you want to do now?" << endl;
      cout << "Choose a number..." << endl;
      cout << "1) Talk." << endl;
      cout << "2) Vent." << endl;
      cout << "3) Play a guessing game." << endl;
      cout << "4) End." << endl;
      cin >> play;
   }

   return 0;
}

The usual way to do something like this is to put your entire code in a while (true) loop, and break out of when the user chooses to exit, like so:执行此类操作的常用方法是将整个代码放入while (true)循环中,并在用户选择退出时中断,如下所示:

#include <iostream>
using namespace std;

int main() 
{
    int play;
    while (true)
    {
        cout << "What do you want to do now?" << endl;
        cout << "Choose a number..." << endl;
        cout << "1) Talk." << endl;
        cout << "2) Vent." << endl;
        cout << "3) Play a guessing game." << endl;
        cout << "4) End." << endl;
        cin >> play;

        if (play == 1)
        {
            // code for Talk...
        }
        else if (play == 2)
        {
            // code for Vent...
        }
        else if (play == 3)
        {
            // code for Play a guessing game....
        }
        else if (play == 4)
        {
             // End
            return 0;
        }
        else
        {
            std::cout << "Expected a number between 1 and 4" << std::endl;
        }
    }
}

EDIT I have also added a test for unrecognised input EDIT You can also use a switch statement, if you prefer the syntax (make sure you don't have fall through unless you really want it) (Note: both codes are equivalent and will probably produce identical assembly)编辑我还为无法识别的输入添加了一个测试编辑你也可以使用 switch 语句,如果你喜欢语法(确保你没有失败,除非你真的想要它)(注意:两个代码是等价的,可能会生产相同的组件)

int main() 
{
    int play;
    while (true)
    {
        cout << "What do you want to do now?" << endl;
        cout << "Choose a number..." << endl;
        cout << "1) Talk." << endl;
        cout << "2) Vent." << endl;
        cout << "3) Play a guessing game." << endl;
        cout << "4) End." << endl;
        cin >> play;

    switch (play)
    {
    case 1:
        // code for Talk...
        break;
    case 2:
        // code for Vent...
        break;
    case 3:
        // code for Play a guessing game....
        break;
    case 4:
        // End
        return 0;
    default:
        std::cout << "Expected a number between 1 and 4" << std::endl;
    }
}

A fancy way to do this is using switch() and here is Why the switch statement and not if-else?一个奇特的方法是使用switch() ,这里是为什么使用 switch 语句而不是 if-else? A switch() can help your program get rid of having a very long nested if-else statements. switch()可以帮助您的程序摆脱非常长的嵌套 if-else 语句。 Look how much beautiful its:看看它有多美:

#include <iostream>

int main()
{
    int play = 0;

    while (play != 4)
    {
        std::cout << "What do you want to do now?" << std::endl;
        std::cout << "Choose a number..." << std::endl;
        std::cout << "1) Talk." << std::endl;
        std::cout << "2) Vent." << std::endl;
        std::cout << "3) Play a guessing game." << std::endl;
        std::cout << "4) End." << std::endl;
        std::cin >> play;

        switch (play)
        {
            case 1: // code for talk here
                break;
            case 2: // code for Vent
                break;
            case 3: // code for Play
                break;
            case 4: std::cout << "program will exits!";
        }
    }
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM