【发布时间】:2015-01-16 19:11:16
【问题描述】:
嗯,我正在写一个骰子游戏。我尝试在这里搜索骰子游戏,但似乎都没有回答我的问题。无论如何,这不是掷骰子的问题。这是关于 do while 循环的。我对这个网站非常陌生,我刚刚通过Maximum PC Magazine 发现了这一点,所以请多多包涵。我也是编程新手。
这是我的代码:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(){
srand(time(NULL));
int userRoll = rand() % 6 + 1 ;
int computerRoll = rand() % 6 + 1 ;
string yesOrNoChoice;
string commandToThrowDie;
do{
cout << "Please enter \"throw\" (lowercase) to roll the die: ";
cin >> commandToThrowDie;
} while(commandToThrowDie != "throw");
do{
cout << "You rolled: " << userRoll << endl
<< "The Computer rolled: " << computerRoll << endl;
if (userRoll < computerRoll){
cout << "You lose. Try again? [Yes/No]: ";
}
if (computerRoll < userRoll){
cout << "You win! Try again? [Yes/No]: ";
}
if (computerRoll == userRoll) {
cout << "It's a draw. Try again? [Yes/No]: ";
}
cin >> yesOrNoChoice;
} while(yesOrNoChoice != "Yes");
system ("pause");
return 0;
}
问题是,在要求用户在 do-while-loop 结束时输入一个选项后,无论我输入什么,程序都会退出循环,而不是循环回到另一个掷骰子。
结果是这样的:
【问题讨论】:
-
您的实际问题是什么?你有什么问题?你尝试了什么?
-
请正确格式化您的代码 - 随机缩进很难阅读。
-
@Tomo 第二个 do-while 循环不起作用。我发现“你滚了:”等等。我不明白为什么它不起作用,它可能是编码的......或者我认为。顺便说一句,我还是编程新手 :)
-
@DylanVillaruel:你有一本关于 C++ 的好书吗?如果没有,请查看此处的列表:stackoverflow.com/questions/388242/…
-
你不应该搜索“骰子游戏”,这是错误的思考方式:这些词不能描述你的编程问题。还要学习在调试器中逐步运行程序,您会注意到问题所在。
标签: c++ loops while-loop do-while dice