2015-11-03 54 views
1

我正在創建一個程序,它需要一個必須添加到兩個骰子上的數字的數字。例如:用戶輸入9然後像6 + 3。這兩個數字隨機生成並保持生成,直到它們等於用戶輸入的數字。 這裏是我的代碼:隨機生成的數字與while循環無法正常工作

#include <stdio.h> 
#include <stdlib.h> 
int main(void) { 

     int totalSought; 
     int count = 0; 
     time_t totalTime; 

     printf("Game of Dice \n"); 
     printf("============ \n"); 

     printf("Enter total sought: "); 
     scanf("%d", &totalSought); 

     //if totalsought is higher than 12 the loop keeps asking to re-enter! 
     if (totalSought > 12) { 

       printf("** Invalid Input! Try Again! **\n"); 

       while (totalSought > 12) { 

         printf("Enter total sought: "); 
         scanf("%d", &totalSought); 
       } 
     } 
     //loads random number generator 
     srand((unsigned) time(&totalTime)); 


     //this loop checks if rand generates 2 random added equal to totalsought- 
     //if not equal they keep generating until it equals! 
     while (rand() % 7 + rand() % 7 != totalSought) { 
       count++; 
       printf("Result of the throw %d: %d \n",count, rand() % 7 + rand() % 7); 
     } 

} 

但是當我編譯並運行它,它不會停止給我輸入的號碼。

Game of Dice 
============ 
Enter total sought: 5 
Result of the throw 1: 5 
Result of the throw 2: 11 
Result of the throw 3: 7 
Result of the throw 4: 4 
Result of the throw 5: 6 

它應該停止在第一次扔,但它沒有。 我該如何解決這個錯誤?

+0

備註:'rand()%7'生成一個從0到6的數字。所以如果總查找次數是5,那麼最終可能會得到0和5的回答,不是骰子的有效投擲。 – user3386109

回答

1

這是由於您如何構建了循環。

//this loop checks if rand generates 2 random added equal to totalsought- 
    //if not equal they keep generating until it equals! 
    while (rand() % 7 + rand() % 7 != totalSought) { 
      count++; 
      printf("Result of the throw %d: %d \n",count, rand() % 7 + rand() % 7); 
    } 

看,你當你做出蘭特()的調用被評估(兩次),然後你又在printf的稱之爲()。

發生什麼事情是while條件中的結果與printf()中的結果不同。

你應該做的是聲明一個變量來存儲你的結果,然後在while條件和下面的printf()中使用該變量。

我希望這是很清楚:)

0

while循環中的值與您打印的值不同。

例如在while循環的價值可能是5 和打印出來的值可能是7

while (rand() % 7 + rand() % 7 != totalSought) { 
       count++; 
       printf("Result of the throw %d: %d \n",count, rand() % 7 + rand() % 7); 
2

循環

while (rand() % 7 + rand() % 7 != totalSought) { 
      count++; 
      printf("Result of the throw %d: %d \n",count, rand() % 7 + rand() % 7); 
    } 

意味着:創建兩個隨機數,將它們添加。如果總和等於totalSought,則打破循環;否則創建兩個(完全不相關的)隨機並打印它們的總和。

+0

從更經常的「我的'rand()'返回相同的數字」的歡迎轉移「 - 在這種情況下,OP假設它總是這樣做。 – usr2564301

2

蘭特的每次調用()返回一個隨機數。 while()語句中的檢查結果與您打印的結果無關。你的代碼的最小變化(不是最好的),我能想到的,使其工作的權利是一樣的東西:

int r1 = rand()%7; 
int r2 = rand()%7; 
while (r1+r2!=totalSought) { 
    count++; 
    printf("Result of the throw %d: %d \n",count, r1 + r2); 
    r1 = rand()%7; 
    r2 = rand()%7; 
} 
+0

'rand()%7'模仿[seven sided die](http://i.imgur.com/V5JChhD。jpg)令人驚訝的存在。 – chux

+0

你爲什麼要做rand()%7?這會給你一個介於0和6之間的數字。你應該(1 + rand()%6)得到1到6之間的數字。 – bruceg

1

有時候是好的,創建處理骰子,而不是錯誤的功能「重滾動「的時候打印。

還有一點性能改進:生成一個0到35的隨機數,然後分成2個6面的骰子。

int SumOf2Dice(int *d1, int *d2) { 
    int d36 = rand()%36; 
    *d1 = d36/6 + 1; 
    *d2 = d36%6 + 1; 
    return *d1 + *d2; 
} 

.... 

int d1, d2; 
while (SumOf2Dice(&d1, &d2) != totalSought) { 
    count++; 
    printf("Result of the throw %d: %d\n",count, d1 + d2); 
}