C++: 程序 "Not Responding"执行时

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

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

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

我的程序没有返回任何错误...但是在尝试执行时它不会工作...现在我担心我有一个损坏的文件或导致 Codeblocks 停止工作的东西。知道发生了什么以及如何让 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;

最佳答案

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

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

关于C++: 程序 "Not Responding"执行时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50546576/

相关文章:

java - 如何将此 Do While 循环转换为另一种循环,即 while 循环?

python - 等待用户输入时运行循环

Python 循环随机数

javascript - Node.js 执行重放日志系统

c++ - map 不可递增错误

c++ - mmap 和 C++ 严格的别名规则

java - 调试 Java Swing App 导致计算机死机

javascript - 即 10 : debug console (F12) changes behavior

c++ - 需要帮助理解返回引用的函数

c++ - 在 C++ 中,相等运算符中两个 = 之间的空格是否合法?