2015-06-21 168 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