Loops 对于骰子游戏是使用do while还是while循环感到困惑
想知道for循环是否更好,或者我是否应该坚持while循环。如果是的话,为什么Loops 对于骰子游戏是使用do while还是while循环感到困惑,loops,while-loop,do-while,Loops,While Loop,Do While,想知道for循环是否更好,或者我是否应该坚持while循环。如果是的话,为什么 srand((unsigned) time(NULL)); bool result; bool end = false; int win, lose; char answer; win = lose = 0; while (!end) { result = play_game(); if (result) { printf("You win!\n\n"); ++wi
srand((unsigned) time(NULL));
bool result;
bool end = false;
int win, lose;
char answer;
win = lose = 0;
while (!end) {
result = play_game();
if (result) {
printf("You win!\n\n");
++win;
} else {
printf("You Lose!\n\n");
++lose;
}
printf("Do you want to play again (y/n) ");
if ((toupper(answer = getchar())) == 'Y') {
end = false;
} else if ((toupper(answer = getchar())) == 'N'){
end = true;
}
}
我会用一段时间,只要你不局限于一定数量的游戏
char answer;
do
{
answer = toupper(getchar());
// ...
} while (answer == 'Y');
这里的区别是do块将至少运行一次。
如果再次满足该条件(
answer==“Y”
),则do块将再次运行,否则它将中断。第12个还是第12个?有时是的。这个问题是关于do-while对while还是for-loop对while循环?标题与文章内容不匹配。语言标签最好。很多语言都有类似C的语法。技术上可能是其中之一。