【问题标题】:C++ Do-while loop stoppingC++ Do-while 循环停止
【发布时间】:2015-09-19 08:26:49
【问题描述】:

我得到了一个任务,我们让一个 cmd 提示符出现并显示一个用于乘法的抽认卡游戏。输入正确答案后,会出现提示并要求用户选择“Again?Y/N”。在第二个输入回答后,询问用户的提示没有出现,并且卡在“祝贺”消息上。当我编写代码为游戏随机生成两个数字时,就会发生这种情况。一个在while循环外,一个在while循环内。如果我为随机数留下第二个代码,它将运行良好,但只会再次显示相同的数字。我要问的是如何解决它,以便在第二个答案输入后不会卡住?

示例代码如下:

#include <iostream>

using namespace std;

int main()
{
    int num1, num2, ans, guess, count = 0;
    char choice;

    num1 = rand() % 12 + 1;  
    num2 = rand() % 12 + 1;
    //first number generator.
    ans = num1 * num2;

    do
    {
        {
            cout << num1 << " X " << num2 << " = ";
            cin >> guess;
            cout << "Wow~! Congratulations~! ";
            count++;

            num1 = rand() % 12 + 1;
            num2 = rand() % 12 + 1;
            //second number generator.

        } while (guess != ans);


        cout << "\nAgain? Y/N: ";
        cin >> choice;

    } while ((choice == 'y') || (choice == 'Y'));
    //after two turns the loop stops. Can't make a choice.

    cout << " Thanks for playing! Number of tries:" << count << endl;

    return 0;
}

【问题讨论】:

  • 您缺少用于 while (guess != ans) 循环的 do 动词。

标签: c++ do-while


【解决方案1】:

我猜这个问题是因为你的循环不是你想象的那样。

do
{

上面的代码已经开始了一个do循环。

    {

我怀疑你打算在这里开始另一个(嵌套的)do 循环——但你离开了do,所以它只是一个进入、执行和退出的块。在这种情况下无用且毫无意义。

        cout << num1 << " X " << num2 << " = ";
        cin >> guess;
        cout << "Wow~! Congratulations~! ";
        count++;

        num1 = rand() % 12 + 1;
        num2 = rand() % 12 + 1;
        //second number generator.

    } while (guess != ans);

您已将其格式化为好像 while 正在关闭嵌套的 do 循环 - 但由于您实际上并未创建嵌套的 do 循环,因此这只是一个带有空的 while 循环身体。稍微重新格式化一下,它的含义会更明显:

    // second number generator
}

while (guess != ans)
    /* do nothing */
    ;

【讨论】:

  • 虽然这解决了问题,但程序(仍然)在语义上不正确,因为它说每个答案都是正确的。
  • 哦,我现在明白了。感谢您与我分享您的知识!
【解决方案2】:

问题可以在这里找到:

do
{
    {
        cout << num1 << " X " << num2 << " = ";
        cin >> guess;

如您所见,第二个作用域没有do 语句。因此它只是一个代码块。

您可以通过为第二个代码块编写do 语句来解决它。

由于第二个括号 ({) 中不存在 do,因此 while 被解释为 while 循环:

while (guess != ans);

while (guess != ans) {
}

这样会一直循环直到guess 不等于ans。但是由于 in 循环不会修改任何两个变量,所以循环会不断迭代。


其他错误:请注意,该程序仍然不正确,因为它会声称您已经回答了问题,而不管答案如何。您可以通过如下实现来修复它:

int main()
{
    int num1, num2, ans, guess, count = 0;
    char choice;

    do {

       num1 = rand() % 12 + 1;
       num2 = rand() % 12 + 1;
       ans = num1 * num2;

       do {
            cout << num1 << " X " << num2 << " = ";
            cin >> guess;
            if(guess == ans) {
                cout << "Wow~! Congratulations~! ";
            } else {
                cout << "No, wrong!\n";
            }
            count++;

        } while (guess != ans);


        cout << "\nAgain? Y/N: ";
        cin >> choice;

    } while ((choice == 'y') || (choice == 'Y'));
    //after two turns the loop stops. Can't make a choice.

    cout << " Thanks for playing! Number of tries:" << count << endl;

    return 0;
}

【讨论】:

  • 哦,哇,我完全忘记了 if 和 else 语句。谢谢!
猜你喜欢
  • 1970-01-01
  • 2018-05-09
  • 2021-03-04
  • 1970-01-01
  • 1970-01-01
  • 2016-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多