if / else循环:C ++程序:不会显示最终的提示/最终循环

时间:2018-05-24 22:13:09

标签: c++ loops debugging if-statement while-loop

我在第一次C ++课程中遇到了很多麻烦。

我已经想出如何让它正确地询问用户他们想要使用什么操作(加,减,乘),生成0-9之间的随机数,以及如何要求用户解决问题和如果它是正确的或不正确的。

在此之后,程序应该询问用户是否要继续(通过按y)或退出(按Q),当用户输入任何其他字母时会显示错误消息,但是某些原因导致程序在运行程序时不显示。

如何让循环正常工作,允许我做最后的提示,然后只有在按Y时重复整个程序或在按Q时退出?

注意:我对编码很新,这是我的第一个C ++课程,所以我还不知道如何使这段代码更简洁:

#include <iostream>
#include <cstdio>
#include <time.h>
#include <stdlib.h>
using namespace std;

int main()

{
    while (true)
   {
  // Generate two random single-digit integers btwn 0-9
  srand(time(0));
  int num1 = rand() % 10;
  int num2 = rand() % 10;
  int operation, play, num3, guess, Y, Q;

  // If num1 < num2, swap num1 with num2
  if (num1 < num2)
  {
  int temp = num1;
  num1 = num2;
  num2 = temp;
  }

cout << "Choose an operation." << endl;
cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: " << endl;
cin >> operation;

    if (operation > 3 || operation < 1)
    {
        cout << "Your operation choice isn't valid!  Please try again, using 1, 2, or 3." << endl;
        cout << "Choose an operation." << endl;
        cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: " << endl;
        cin >> operation;
    }

     else if (operation == 1)
        {
        cout << "You chose addition." << endl;
        num3 = num1 + num2;
        cout << "What is " <<  num1 << " + " << num2 << " ?: " << endl;
        cin >> guess;

            if (guess != num3)
                {
                cout << "That is incorrect. Please try again." << endl;
                cout << "" << endl;
                cout << "What is " <<  num1 << " + " << num2 << " ?: " << endl;
                cin >> guess;
                }

            else if (guess == num3)
                {
                cout << "That is correct!" << endl;
                cout << "" << endl;
                }
        }

     else if (operation == 2)
        {
        cout << "You chose subtraction." << endl;
        num3 = num1 + num2;
        cout << "What is " <<  num1 << " - " << num2 << " ?: " << endl;
        cin >> guess;

            if (guess != num3)
                {
                cout << "That is incorrect. Please try again." << endl;
                cout << "" << endl;
                cout << "What is " <<  num1 << " - " << num2 << " ?: " << endl;
                cin >> guess;
                }

            else if (guess == num3)
                {
                cout << "That is correct!" << endl;
                cout << "" << endl;
                }
        }

     else if (operation == 3)
        {
        cout << "You chose multiplication." << endl;
        num3 = num1 * num2;
        cout << "What is " <<  num1 << " * " << num2 << " ?: " << endl;
        cin >> guess;

            if (guess != num3)
                {
                cout << "That is incorrect. Please try again." << endl;
                cout << "" << endl;
                cout << "What is " <<  num1 << " * " << num2 << " ?: " << endl;
                cin >> guess;
                }

            else if (guess == num3)
                {
                cout << "That is correct!" << endl;
                cout << "" << endl;
                }
        }

while (guess != num3)
         {
         int play, Y, Q;
         cout << "Would you like to play again? Press Y for yes or Q for 
quit" << endl;
         cin >> play;

           if (play != Y || play != Q)
            {
                cout << "That is not a valid choice. Please choose Y for yes 
or Q to quit. " << endl;
                cin >> play;
            }

            else
            {
                if (play == Y)
                {
                cout << "Thank you for playing! Let's play again!" << endl;
                cout << "" << endl;
                }


                else if (play == Q)
                {
                cout << "Thank you for playing! See you next time!" << endl;
                cout << "" << endl;
                }
                break;
                }
         }
         }
return 0;
}

5 个答案:

答案 0 :(得分:2)

这里的事情很少......

1。只播种一次

srand(time(0));移出while循环并移至main的顶部。如果您在同一秒内重复播种(如果time(0)没有改变),您将获得两次相同的“随机”数字。

2。 num3如果他们没有输入有效的operation

,会发生什么?

您永远不会num3初始化,因此如果他们没有选择有效的operationnum3会有垃圾值。然后你继续运行一个循环,其条件取决于num3的值! (while (guess != num3)

3。 else { ... if {else if {

相同

在最后一个循环中,将if (play == Y)else if (play == Q)从嵌套if中取出,并将其设为else if

4。您的上一个循环条件不正确

while (guess != num3)真的对吗?你想循环,直到他们输入有效的输入,那么为什么你在guess != num3时循环?

答案 1 :(得分:1)

使用开关盒可以更好地选择正确的操作:

Switch(operation) {case 1: break;}

您还需要添加一个 所以正确的代码应该是这样的:

       #include <iostream>
#include <cstdio>
#include <time.h>
#include <stdlib.h>
using namespace std;

int main()

{
        int operation, num3, guess,num1,num2,temp;
        srand(time(0));
        char play;
        do
        {
        // Generate two random single-digit integers btwn 0-9
          num1 = rand() % 10;
          num2 = rand() % 10;

          // If num1 < num2, swap num1 with num2
          if (num1 < num2)
          {
              temp = num1;
              num1 = num2;
              num2 = temp;
          }
            do
            {
                cout << "Choose an operation." << endl;
                cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: " << endl;
                cin >> operation;

                if (operation > 3 || operation < 1)
                {
                    cout << "Your operation choice isn't valid!  Please try again, using 1, 2, or 3." << endl;
                }
            }while(operation>3 || operation<1);
            switch(operation)
            {
                case 1:
                cout << "You chose addition." << endl;
                num3 = num1 + num2;
                cout << "What is " <<  num1 << " + " << num2 << " ?: " << endl;
                cin >> guess;

                    if (guess != num3)
                        {
                        cout << "That is incorrect. Please try again." << endl;
                        cout << "" << endl;
                        cout << "What is " <<  num1 << " + " << num2 << " ?: " << endl;
                        cin >> guess;
                        }

                    else if (guess == num3)
                        {
                        cout << "That is correct!" << endl;
                        cout << "" << endl;
                        }
                break;
                case 2:
                    cout << "You chose subtraction." << endl;
                    num3 = num1 - num2;
                    cout << "What is " <<  num1 << " - " << num2 << " ?: " << endl;
                    cin >> guess;

                        if (guess != num3)
                            {
                            cout << "That is incorrect. Please try again." << endl;
                            cout << "" << endl;
                            cout << "What is " <<  num1 << " - " << num2 << " ?: " << endl;
                            cin >> guess;
                            }

                        else if (guess == num3)
                            {
                            cout << "That is correct!" << endl;
                            cout << "" << endl;
                            }
                    break;
                case 3:
                    cout << "You chose multiplication." << endl;
                    num3 = num1 * num2;
                    cout << "What is " <<  num1 << " * " << num2 << " ?: " << endl;
                    cin >> guess;

                        if (guess != num3)
                            {
                            cout << "That is incorrect. Please try again." << endl;
                            cout << "" << endl;
                            cout << "What is " <<  num1 << " * " << num2 << " ?: " << endl;
                            cin >> guess;
                            }

                        else if (guess == num3)
                            {
                            cout << "That is correct!" << endl;
                            cout << "" << endl;
                            }
                break;
            }
            do
            {
                 cout << "Would you like to play again? Press Y for yes or Q for quit" << endl;
                cin >> play;
               if (play != 'Y' && play != 'Q')
                {
                    cout << "That is not a valid choice. Please choose Y for yes or Q to quit. " << endl;
                }
            }while(play!='Y' && play!='Q');
            if (play == 'Y')
            {
            cout << "Thank you for playing! Let's play again!" << endl;
            cout << "" << endl;
            }
            else
            {
            cout << "Thank you for playing! See you next time!" << endl;
            cout << "" << endl;
            }
         }while(play=='Y');
return 0;
}

答案 2 :(得分:1)

所以正确的代码是:

PS F:\Nick\Angular\firebase-work> npm update @angular/core
    Package "@angular/flex-layout" has an incompatible peer dependency to "rxjs" (requires "^5.5.0, would install "6.2.0").
Incompatible peer dependencies found. See above.
PS F:\Nick\Angular\firebase-work> npm view rxjs version
6.2.0
PS F:\Nick\Angular\firebase-work> npm view rxjs -g version
6.2.0
F:\Nick\Angular\firebase-work>

答案 3 :(得分:1)

问题出现在第二个while-loop中。 play变量应声明为char而不是int。另外,您不需要将其与YQ整数变量进行比较。这是一个解决方案。我跳它会帮助你:

#include <iostream>
#include <cstdio>
#include <time.h>
#include <stdlib.h>
using namespace std;

int main()
{
    bool loop = true;
    while (loop)
    {
        // Generate two random single-digit integers btwn 0-9
        srand(time(0));
        int num1 = rand() % 10;
        int num2 = rand() % 10;
        int operation, play, num3, guess, Y, Q;

        // If num1 < num2, swap num1 with num2
        if (num1 < num2)
        {
            int temp = num1;
            num1 = num2;
            num2 = temp;
        }

        cout << "Choose an operation.\n\t-----------------------" << endl;
        cout << "\tEnter 1 to add,\n\tEnter 2 to subtract, or\n\tEnter 3 to multiply\n\t-----------------------\n\t\tEnter: ";
        cin >> operation;

        if (operation > 3 || operation < 1)
        {
            cout << "Invalid choice! Please try again." << endl;
            continue;
        }
        else if (operation == 1)
        {
            cout << "You chose addition." << endl;
            num3 = num1 + num2;
            cout << "What is " <<  num1 << " + " << num2 << " = ";
            cin >> guess;
            if (guess != num3)
            {
                cout << "That is incorrect. Please try again." << endl;
                cout << "What is " <<  num1 << " + " << num2 << " = ";
                cin >> guess;
            }
            else if (guess == num3)
                cout << "That is correct!" << endl;
        }
        else if (operation == 2)
        {
            cout << "You chose subtraction." << endl;
            num3 = num1 + num2;
            cout << "What is " <<  num1 << " - " << num2 << " = ";
            cin >> guess;

            if (guess != num3)
            {
                cout << "That is incorrect. Please try again." << endl;
                cout << "What is " <<  num1 << " - " << num2 << " = ";
                cin >> guess;
            }
            else if (guess == num3)
                cout << "That is correct!" << endl;
        }
        else if (operation == 3)
        {
            cout << "You chose multiplication." << endl;
            num3 = num1 * num2;
            cout << "What is " <<  num1 << " * " << num2 << " = ";
            cin >> guess;
            if (guess != num3)
            {
                cout << "That is incorrect. Please try again." << endl;
                cout << "What is " <<  num1 << " * " << num2 << " = ";
                cin >> guess;
            }
            else if (guess == num3)
                cout << "That is correct!" << endl;
        }

        while (true)
        {
            char play;
            cout << "Would you like to play again? Press Y for yes or Q for quit: ";
            cin >> play;
            if (play == 'Y' || play == 'y')
                break;
            else if(play == 'Q' || play == 'q')
            {
                loop = false;
                cout << "Good bye.\n";
                break;
            }
            else
                cout<< "Invalid choice.\n";
        }
    }
    return 0;
}

使菜单有点互动也很好,和平。

答案 4 :(得分:0)

解决方案:

P29:练习算术技巧(if / else,loop)

说明

“编写一个让孩子练习算术技能的程序。

程序应首先询问需要什么样的练习:+, - ,*,并让用户根据需要多次重复练习,直到输入“Q”。

将从(0 - 9)生成两个随机数。

如果孩子正确回答等式,则应出现一条消息,然后他们可以转到下一个问题(生成两个不同的数字)。

如果孩子的答案不正确,则应显示一条消息&amp;问题应该重复(使用相同的数字)。“

最后修好了!

    #include <iostream>
    #include <cstdio>
    #include <time.h>
    #include <stdlib.h>
    using namespace std;

    int main()
   {
    int operation, num3, guess, num1, num2, temp;
    char play;
    srand(time(0));

    do
    {
      num1 = rand() % 10;
      num2 = rand() % 10;

      if (num1 < num2)
      {
          temp = num1;
          num1 = num2;
          num2 = temp;
      }

        do
        {
            cout << "Choose an operation." << endl;
            cout << "Enter 1 to add, 2 to subtract, or 3 to multiply: " << endl;
            cout << "" << endl;
            cin >> operation;

            if (operation > 3 || operation < 1)
            {
                cout << "Your operation choice isn't valid!  Please try again, using 1, 2, or 3." << endl;
            }
        }while (operation > 3 || operation < 1);

        switch(operation)
        {
            case 1:
            cout << "You chose addition." << endl;
            num3 = num1 + num2;
            cout << "" << endl;

            do
            {
                cout << "What is " <<  num1 << " + " << num2 << " ?: " << endl;
                cout << "" << endl;
                cin >> guess;
                cout << "" << endl;

                if (guess != num3)
                    {
                    cout << "That is incorrect. Please try again." << endl;
                    cout << "" << endl;
                    }
            } while (guess != num3);

                if (guess == num3)
                    {
                    cout << "That is correct!" << endl;
                    cout << "" << endl;
                    }
            break;

            case 2:
            cout << "You chose subtraction." << endl;
            num3 = num1 - num2;
            cout << "" << endl;

            do
            {
                cout << "What is " <<  num1 << " - " << num2 << " ?: " << endl;
                cout << "" << endl;
                cin >> guess;
                cout << "" << endl;

                if (guess != num3)
                    {
                    cout << "That is incorrect. Please try again." << endl;
                    cout << "" << endl;
                    }
            } while (guess != num3);

                if (guess == num3)
                    {
                    cout << "That is correct!" << endl;
                    cout << "" << endl;
                    }
            break;

            case 3:
            cout << "You chose multiplication." << endl;
            num3 = num1 * num2;
            cout << "" << endl;

            do
            {
                cout << "What is " <<  num1 << " * " << num2 << " ?: " << endl;
                cout << "" << endl;
                cin >> guess;
                cout << "" << endl;

                if (guess != num3)
                    {
                    cout << "That is incorrect. Please try again." << endl;
                    cout << "" << endl;
                    }
            } while (guess != num3);

                if (guess == num3)
                    {
                    cout << "That is correct!" << endl;
                    cout << "" << endl;
                    }
            break;
        }

        do
        {
             cout << "Would you like to play again? Press Y for yes or Q for quit" << endl;
             cout << "" << endl;
             cin >> play;

           if (play != 'Y' && play != 'Q')

            {
                cout << "That is not a valid choice. Please choose Y for yes or Q to quit. " << endl;
                cout << "" << endl;
            }

        }

        while(play !='Y' && play !='Q');

        if (play == 'Y')
        {
        cout << "Thank you for playing! Let's play again!" << endl;
        cout << "" << endl;
        }

        else
        {
        cout << "Thank you for playing! See you next time!" << endl;
        cout << "" << endl;
        }

     }
     while(play=='Y');

    return 0;
    }