基本上我應該模擬一個程序,擲三個骰子,加起來。然後,我會讓用戶猜測下一輪是更高,更低,相同還是他們只想退出。我有兩個問題。骰子游戲數字不隨機
數字不是隨機的,顯然這是我的一個很大的錯誤,但我似乎無法弄清楚。我想我不需要第二次加入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