C++ do/while 循环和 if/else: 骰子游戏 -- 第 75 行错误:"Cout"之前的预期"While" -- 如何解决这个问题?



初级程序员 -- 第一堂C++课。

我从第 75 行开始不断收到错误,我无法弄清楚我哪里出错了。我知道我的代码很粘...我需要帮助清理它。有人知道我如何正确编码吗?

这是作业:

骰子(如果/否则,循环,随机(:

使用 if/else、循环和随机数编写一个骰子游戏程序,用户在其中掷 2 个骰子并决定他们是要"持有"这些数字还是再次掷骰子。它应该让用户随心所欲地滚动多次,直到他们保持。

然后计算机也应该掷 2 个骰子。游戏的目的是获得比计算机更高的分数。

===

提示:使用随机数黑白1-6,制作4个随机数

当我尝试构建程序时,我遇到了一堆错误,并且真的不知道我做错了什么(试图调试自己(。以下是错误:

||=== Build file: "no target" in "no project" (compiler: unknown) ===|
||In function 'int main()':|
|75|error: expected 'while' before 'cout'|
|75|error: expected '(' before 'cout'|
|75|error: expected ')' before ';' token|
|87|error: expected 'while' before 'While'|
|87|error: expected '(' before 'While'|
|87|error: 'While' was not declared in this scope|
|87|error: expected ')' before ';' token|
||=== Build failed: 7 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

这是我到目前为止的代码 - 任何人都可以告诉我正确的代码吗?

#include <iostream>
#include <cstdio>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
{
srand(time(0));
int Roll1 = 1+(rand()%6);
int Roll2 = 1+(rand()%6);
int UserRoll = Roll1 + Roll2;
int Roll3 = 1+(rand()%6);
int Roll4 = 1+(rand()%6);
int ComputerRoll = Roll3 + Roll4;
string Hold;
string ThrowDice;
string Again;
do
{
do
{
cout << "Please enter "throw" (lowercase) to roll the dice: ";
cin >> ThrowDice;
} while(ThrowDice != "throw");
do
{
cout << "You rolled: " << Roll1 << " and " << Roll2 << endl;
cout << "Would you like to hold onto these numbers or roll again? " << endl;
cout << "Enter (lowercase) "hold" to keep this roll, or "throw" to roll again): ";
cin >> Hold;
if (Hold != "hold" && Hold != "throw")
{
cout << "Your choice isn't valid!  Please try again by typing "hold" or "throw": " << endl;
}
}while (Hold != "hold" && Hold != "throw");
do
{
if (Hold == "hold")
{
cout << "You chose to keep these numbers, totaling: " << UserRoll << endl;
cout << "The computer rolled " << Roll3 << " and " << Roll4 << " totaling: " << ComputerRoll << endl;
} break;
if (Hold == "throw")
{
cout << "You chose to roll again. " << endl;
}
}while (Hold == "throw");
do
{
if (UserRoll < ComputerRoll)
{
cout << "Sorry. You lose. " << endl;
} break;
if (ComputerRoll < UserRoll)
{
cout << "Congratulations! You win! " << endl;
} break;
if (ComputerRoll == UserRoll)
{
cout << "It's a draw. " << endl;
} break;
}
cout << "Would you like to play again? [Yes/No]: " << endl;
cin >> Again;
if (Again == "Yes")
{
continue;
}
else if (Again == "No")
{
break;
}
}While (Again == "Yes");
return 0;
}

最后一个do代码块在:)后需要 while (( 条件

相关内容

最新更新