基本上我应该模拟一个程序,掷三个骰子,加起来。然后,我会让用户猜测下一轮是更高,更低,相同还是他们只想退出。我有两个问题。骰子游戏数字不随机
数字不是随机的,显然这是我的一个很大的错误,但我似乎无法弄清楚。我想我不需要第二次加入3个骰子对?反正他们根本没有帮助。
无论如何,我的程序一次全部通过if/else if语句。很明显,我不希望发生这种情况。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int diceOne, diceTwo, diceThree, diceSum=0, timesCorrect=0, choice;
int newDiceOne, newDiceTwo, newDiceThree, newDiceSum;
srand(time(NULL));
diceOne = rand() % 6 + 1;
diceTwo = rand() % 6 + 1;
diceThree = rand() % 6 + 1;
newDiceOne = rand() % 6 + 1;
newDiceTwo = rand() % 6 + 1;
newDiceThree = rand() % 6 + 1;
printf("The three dice rolls: %d, %d, %d", diceOne, diceTwo, diceThree);
diceSum = diceOne + diceTwo + diceThree;
printf("\nTotal sum of dice is %d\n", diceSum);
do {
printf("Guess higher(1), lower(2), same(3) or quit?(4)\n");
printf(" You have been correct %d times\n", timesCorrect);
scanf("%d", &choice);
printf("The three dice rolls: %d, %d, %d", newDiceOne, newDiceTwo, newDiceThree);
newDiceSum= newDiceOne + newDiceTwo + newDiceThree;
printf("\nTotal sum of dice is %d\n", newDiceSum);
if (choice == 1)
{
if (newDiceSum > diceSum);
timesCorrect++;
printf("You are correct!\n");
}
else if (newDiceSum < diceSum);
{
printf("You are incorrect, sorry!\n");
}
if (choice == 2)
{
if (newDiceSum < diceSum);
timesCorrect++;
printf("You are correct!\n");
}
else if (newDiceSum > diceSum);
{
printf("You are incorrect, sorry!\n");
}
if (choice == 3)
{
if (newDiceSum == diceSum);
timesCorrect ++;
printf("You are correct!\n");
}
else if (newDiceSum != diceSum);
{
printf("You are incorrect, sorry!\n");
}
if (choice == 4)
{
printf("Thanks for playing!!!!!!\n");
system("pause");
return 0;
}
} while (choice!= 4);
}
请缩进代码,并且不使用'系统(“暂停”)的任何研究;'如果它在你的课本,那么请放弃它,如果你的老师使用它,然后改变你的老师。 –
你在哪里掷骰子?不在循环中 –
请注意,数字将不会均匀分布,因为'rand()'很可能会返回一个模2 ** n的值。 – Olaf