[英]Dice Game Numbers not Random
Basically I am supposed to simulate a program that rolls three dice, add it up. 基本上我应该模拟一个掷三个骰子的程序,加起来。 Then I will have the user guess if the next roll is higher, lower, same or if they just want to quit.
然后,我将让用户猜测下一卷是更高,更低,相同还是只是想退出。 I am having two problems.
我有两个问题。
The numbers are not random, obviously this is a big mistake on my part but I can't seem to figure it out. 这些数字不是随机的,显然这对我来说是个很大的错误,但我似乎无法弄清楚。 I am thinking I do not need the second inclusion of 3 dice pairs?
我想我不需要第二个包含3个骰子对吗? They don't help at all anyways.
他们仍然没有帮助。
Regardless, my program goes through all of the if/else if statements all at once. 无论如何,我的程序一次完成所有if / else if语句。 Obviously I do not want this to happen.
显然我不希望这种情况发生。
#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 );
}
You have an extra semicolon after the else if
conditionals, like here else if
有条件,您会在else if
后面加上一个多余的分号,例如这里
else if (newDiceSum < diceSum);
/* ^ this should not be here */
If you use a compiler with good diagnostic capabilities and enable warnings, it should warn you about that " typo ", if you want to leave the block empty use braces like 如果您使用具有良好诊断能力的编译器并启用警告,则应该警告您“ 错字 ”,如果您想将该块留空,请使用大括号,例如
else if (newDiceSum < diceSum) {}
Also, you set the first dice with rand()
and they are random values, but then you always use the same values in the loop. 另外,您可以使用
rand()
设置第一个骰子,它们是随机值,但是在循环中始终使用相同的值。
The following code: 如下代码:
Handles error conditions 处理错误条件
Eliminates unneeded variables 消除不必要的变量
Cleanly compiles 干净地编译
Executes with out failing 执行成功
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ( void )
{
int diceOne;
int diceTwo;
int diceThree;
int diceSum=0;
int timesCorrect=0;
int choice;
int newDiceSum;
srand( time(NULL) );
diceOne = rand() % 6 + 1;
diceTwo = rand() % 6 + 1;
diceThree = 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);
if( 1 != scanf("%d", &choice) )
{ // then scanf failed
perror( "scanf for choice failed" );
exit( EXIT_FAILURE );
}
// implied else, scanf successful
diceOne = rand() % 6 + 1;
diceTwo = rand() % 6 + 1;
diceThree = rand() % 6 + 1;
printf("The three dice rolls: %d, %d, %d", diceOne, diceTwo, diceThree);
newDiceSum = diceOne + diceTwo + diceThree;
printf("\nTotal sum of dice is %d\n", newDiceSum);
switch( choice )
{
case 1:
if (newDiceSum > diceSum)
{
timesCorrect++;
printf("You are correct!\n");
}
else
{
printf("You are incorrect, sorry!\n");
}
break;
case 2:
if (newDiceSum < diceSum)
{
timesCorrect++;
printf("You are correct!\n");
}
else
{
printf("You are incorrect, sorry!\n");
}
break;
case 3:
if (newDiceSum == diceSum)
{
timesCorrect ++;
printf("You are correct!\n");
}
else
{
printf("You are incorrect, sorry!\n");
}
break;
case 4:
printf("Thanks for playing!!!!!!\n");
system("pause");
break;
default:
printf( " invalid choice, valid choices are 1...4\n");
break;
} // end switch
} while (choice!= 4 );
return(0);
} // end function: main
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.