gpt4 book ai didi

c - 骰子游戏数字不是随机的

转载 作者:太空宇宙 更新时间:2023-11-04 08:25:02 25 4
gpt4 key购买 nike

基本上我应该模拟一个掷三个骰子的程序,把它加起来。然后我会让用户猜测下一卷是更高、更低、相同还是他们只是想退出。我有两个问题。

  1. 数字不是随机的,显然这是我的一个大错误,但我似乎无法弄清楚。我在想我不需要第二次包含 3 个骰子对?无论如何,他们一点帮助也没有。

  2. 无论如何,我的程序会同时处理所有 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 );
}

最佳答案

else if 条件句之后有一个额外的分号,就像这里一样

else if (newDiceSum < diceSum); 
/* ^ this should not be here */

如果您使用具有良好诊断能力的编译器并启用警告,它应该警告您有关“错别字”的信息,如果您想将 block 留空,请使用像这样的大括号

else if (newDiceSum < diceSum) {}

此外,您使用 rand() 设置了第一个骰子,它们是随机值,但是您总是在循环中使用相同的值。

关于c - 骰子游戏数字不是随机的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30960630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com