2015-06-21 92 views
0

基本上我应该模拟一个程序,掷三个骰子,加起来。然后,我会让用户猜测下一轮是更高,更低,相同还是他们只想退出。我有两个问题。骰子游戏数字不随机

  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); 
} 
+3

请缩进代码,并且不使用'系统(“暂停”)的任何研究;'如果它在你的课本,那么请放弃它,如果你的老师使用它,然后改变你的老师。 –

+0

你在哪里掷骰子?不在循环中 –

+1

请注意,数字将不会均匀分布,因为'rand()'很可能会返回一个模2 ** n的值。 – Olaf

回答

2

你有else if条件语句后,一个额外的分号,喜欢这里

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

如果使用具有良好的诊断能力编译器并启用的警告,但应该提醒你注意那“排字错误”,如果你想离开块空使用花括号,如

else if (newDiceSum < diceSum) {} 

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

+1

和缩进将有助于 –

+0

谢谢。我知道,所以我不知道为什么我在那里放置了分号。但是,它仍然会通过所有3例如你是正确的!你是不正确的,对不起你对不对,对不起。 – Session

0

以下代码:

  1. 处理错误条件

  2. 消除不需要的变量

  3. 完全编译

  4. 执行具有出失败


#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 
+0

最初我提出了你的答案,但后来我注意到你没有解释为什么OP代码失败,还有什么是“返回(0)”?你使用它就好像它是一个功能! –

+0

谢谢!请问如何把diceOne = rand()%6 + 1; diceTwo = rand()%6 + 1; diceThree = rand()%6 + 1;在循环中,它的工作?我的意思是它完美地工作,但我想从我的错误中学习,而不是从我的错误中学习,而不是让它工作。 – Session