【发布时间】:2015-08-25 22:30:46
【问题描述】:
我想创建一个骰子游戏,但它总是说我的猜测是错误的,我不知道为什么。 我终于明白如何使 rand 函数实际上是随机的 btw :D 我认为这是因为数组没有正确存储字符串。
int main()
{
srand(time(NULL));
int dice1 = 1, dice2 = 1, dice3 = 1, sum, key = 0;
int dice4 = 1, dice5 = 1, dice6 = 1, sum2;
char randomnumber[10];
printf("Diceroll-game \n\n");
printf("The first sum is:\n");
dice1 = (rand()%6 + 1);
dice2 = (rand()%6 + 1);
dice3 = (rand()%6 + 1);
sum = dice1 + dice2 + dice3;
printf("%d\n", sum);
printf("Will the next sum of 3 dices be higher(hi), lower (lo) or the same(sa) (hi, lo, sa)?\n");
printf("Type hi, lo or sa\n");
scanf("%c \n", &randomnumber);
dice4 = (rand()%6 + 1);
dice5 = (rand()%6 + 1);
dice6 = (rand()%6 + 1);
sum2 = dice4 + dice5 + dice6;
if (randomnumber == "hi" && sum2 > sum) {
printf("You were right !\n\a");
}else {
if (key == 0) {
printf("You were wrong.");
key = 1;
}
}
if (randomnumber == "lo" && sum2 < sum) {
printf("You were right !\n\a");
}else {
if (key == 0) {
printf("You were wrong.");
key = 1;
}
}
if (randomnumber == "sa" && sum2 == sum) {
printf("You were right !\n\a");
}else {
if (key == 0) {
printf("You were wrong.");
key = 1;
}
}
printf("\nIt's %d", sum2);
return 0;
谁能帮帮我。 谢谢。
【问题讨论】:
-
我仍然有 key 变量,它现在说“你错了。你是对的!”
-
Bucky 创建了可怕的教程和练习。给自己一本书,伙计!附言
rand是伪随机数生成器,而不是真正的随机数生成器。 -
@undefinedbehaviour ,“巴基”是谁?
-
@CoolGuy Bucky 似乎是this challenge 的起源,介于poor tutorials and undefined behaviour (e.g. buffer overflows) 之间...不要费心去指出他的任何问题;他创造的任何东西都不会有问题;)