Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 对于骰子游戏是使用do while还是while循环感到困惑_Loops_While Loop_Do While - Fatal编程技术网

Loops 对于骰子游戏是使用do while还是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

想知道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");
        ++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的语法。技术上可能是其中之一。