【问题标题】:C++: Program "Not Responding" When ExecutedC++:执行时程序“无响应”
【发布时间】:2018-05-26 20:04:40
【问题描述】:

我正在处理这个 C++ 家庭作业,当我尝试执行时,.exe 文件一直显示“无响应”。我关闭了程序和代码块。当我尝试打开另一个我知道可以重新开始我的作业并重试的程序时,Codeblocks 停止工作。现在,当我尝试打开它时,它一直说“无响应”。

程序应该为用户掷 2 个骰子,用户决定是否要保留骰子数量,一旦用户完成最后一次掷骰,它应该将用户的最终掷骰总数与计算机的总点数——谁高谁赢。

我的程序没有返回错误...但是在尝试执行时它将无法正常工作...现在我担心我的文件已损坏或导致 Codeblocks 停止工作。任何想法发生了什么以及如何让代码块再次正常运行?

这是程序:

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

int main()
{
srand(time(0));

int Roll1, Roll2, UserRoll, Roll3, Roll4, ComputerRoll;
char Hold;
char PlayAgain;

    do
    {
      Roll1 = 1+(rand()%6);
      Roll2 = 1+(rand()%6);
      UserRoll = Roll1 + Roll2;

        do
        {
            cout << "Beat the computer!" << endl;
            cout << "" << endl;
            cout << "You rolled a " << Roll1 << " and a " << Roll2 << "(total= " << UserRoll << ")" << endl;
            cout << "" << endl;
            cout << "Do you want to keep those? (Y/N): " << endl;
            cin >> Hold;

            if (Hold == 'N')
            {
                cout << "Rolling again! " << endl;
            }
        } while (Hold == 'N');

            if (Hold == 'Y')
            {
                cout << "You chose to keep your numbers. Your total is: " << UserRoll << endl;
            } break;

    }  while (true);

    do
    {
      Roll3 = 1+(rand()%6);
      Roll4 = 1+(rand()%6);
      ComputerRoll = Roll3 + Roll4;

        do
        {
            cout << "" << endl;
            cout << "The computer rolled a " << Roll3 << " and a " << Roll4 << endl;
            cout << "" << endl;
            cout << "(total= " << ComputerRoll << ")" << endl;

                if (ComputerRoll < UserRoll)
                {
                cout << "Congratulations, you win! " << endl;
                }
                break;

                if (ComputerRoll > UserRoll)
                {
                cout << "Sorry, you lose. " << endl;
                }
                break;

                if (ComputerRoll == UserRoll)
                {
                cout << "You tied. " << endl;
                }
                break;
        } while (true);
    } while (true);
return 0;

}

【问题讨论】:

    标签: c++ loops debugging while-loop do-while


    【解决方案1】:

    它“没有响应”,因为你的程序是一个无限循环:

    do
    {
      do
      {
        ...
        break; //exit this inner loop
      } while(true);
      // will continue here 
    } while(true); // no way to get out!
    

    【讨论】:

      【解决方案2】:

      查看您的循环。您在 if 语句之外使用 break。因此,您会在第一个 if 之后立即中断您的 do...while 循环。

      【讨论】:

        猜你喜欢
        • 2021-10-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-09
        • 1970-01-01
        • 1970-01-01
        • 2018-09-30
        相关资源
        最近更新 更多