基本上我应该模拟一个滚动三个骰子的程序,加起来。然后我会让用户猜测下一个滚动是高,低,相同还是只是想退出。我有两个问题。
这些数字不是随机的,显然这对我来说是一个很大的错误,但我似乎无法弄明白。我想我不需要第二次包含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 );
}
答案 0 :(得分:2)
else if
条件之后你有一个额外的分号,就像这里
else if (newDiceSum < diceSum);
/* ^ this should not be here */
如果你使用具有良好诊断功能的编译器并启用警告,它应该警告你关于那个&#34; 拼写错误&#34;,如果你想把这个块留空,请使用大括号
else if (newDiceSum < diceSum) {}
另外,你用rand()
设置第一个骰子并且它们是随机值,但是你总是在循环中使用相同的值。
答案 1 :(得分:0)
以下代码:
处理错误情况
消除不必要的变量
干净地编译
执行失败
#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