我应该做一个骰子游戏,玩多次。我不是真的在寻找答案,而是在寻找我做错了什么。我希望for循环将int I赋值为0,然后运行骰子滚动,然后向I添加一个,直到I>50。提前谢谢。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

main(){
int rollDie1, rollDie2, keyValue = 0, win = 0, lose = 0, reroll = 0, i;
srand(time(NULL));  // call this only once – seed the random generator

for (i = 0 ; i < 50 ; i++);
{
rollDie1 = rand() % 6 + 1;  // in the range of 1 - 6
rollDie2 = rand() % 6 + 1;  // in the range of 1 - 6
keyValue = rollDie1 + rollDie2;



    if ( keyValue == 7 || keyValue == 11 )
    {
    printf ("Player wins on the first roll \n");
    }


    if ( keyValue == 2 || keyValue == 3 || keyValue == 12 )
    {
    printf("Sorry, you lost on the first roll \n");
    }

    if (  keyValue == 4 || keyValue == 5 || keyValue == 6 || keyValue == 8 || keyValue == 9 || keyValue == 10 )
    {
    printf("Reroll! \n");
    }
}

system("pause");
}

最佳答案

for循环的末尾有一个不应该存在的;

for (i = 0 ; i < 50 ; i++);

应该是
for (i = 0 ; i < 50 ; i++)

否则for循环中没有任何内容,{}中的内容将只执行一次,因为这是一个单独的语句。

关于c - 刚开始学习c for循环,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16745436/

10-11 02:37