2015-07-01 84 views
0

我正在爲C類的介紹製作一個加/乘遊戲。該程序的目的是要求用戶,你要使用的最大數量是什麼,它將在最大範圍內播種隨機數,以及你想要做多少不同的問題。當我運行程序並進行數學運算時,它告訴我這個總和是不正確的,並且給我提供了一個不正確的答案,通常是一個很大的數字,例如「1254323」。你能指出我正確的方向,我做錯了什麼?隨機數(加法遊戲)問題

#include <stdio.h> 
#include <time.h> 
#include <stdlib.h> 

int main() { 

    int maxNumber, num1, num2, sum, answer, problems, i; 

    srand(time(NULL)); 

    //printf("Would you like 1)Addition or 2)Multiplication?\n") 
    printf("Enter your Max Number:"); 
    scanf("%d", &maxNumber); 

    printf("How many problems do you want?\n"); 
    scanf("%d", &problems); 

    sum = num1 + num2; 

    while(i != problems) { 
     num1 = rand()%maxNumber; 
     num2 = rand()%maxNumber; 
     i++; 

     printf("What is %d + %d\n",num1, num2); 
     scanf("%d", &answer); 

     if(answer != sum){ 
      printf("Sorry, that's incorrect, the answer is %d\n", sum); 
     } 

     else{ 
      printf("Correct!\n"); 
     } 
    } 

    return 0; 
} 
+0

請注意(甚至在您將'sum = num1 + num2'行加入到循環中之後,您正在生成_undefined behaviour__。Read [here](http://stackoverflow.com/questions/30821406/why-does- rand-rand-produce-negative-numbers/30821874#30821874)。 – Olaf

回答

1

要設置num1num2之前分配sum

sum = num1 + num2;行移入num2 = rand()%maxNumber;行後的循環中,它應該可以正常工作。

還有一些其他錯誤(如初始化i與0)。

順便說一句,通常認爲使用for循環代替while循環更好。

這裏稍微可讀的代碼

#include <stdio.h> 
#include <time.h> 
#include <stdlib.h> 

int main(){ 
    int maxNumber, num1, num2, sum, answer, problems, i; 
    srand(time(NULL)); 

    printf("Enter your Max Number:"); 
    scanf("%d", &maxNumber); 

    printf("How many problems do you want?\n"); 
    scanf("%d", &problems); 

    for (i = 0; i < problems; i++) { 
    num1 = rand()%maxNumber; 
    num2 = rand()%maxNumber; 
    sum = num1 + num2; 

    printf("What is %d + %d\n",num1, num2); 
    scanf("%d", &answer); 

    if(answer != sum){ 
     printf("Sorry, that's incorrect, the answer is %d\n", sum); 
    } else { 
     printf("Correct!\n"); 
    } 
    } 

    return 0; 
} 
+0

啊,愚蠢的錯誤,非常感謝你Jay。 –

+0

'maxNumber'永遠不會被這個mod除外'num1 = rand()%(maxNumber + 1 );' –

1

您在使用變量,但不初始化它們。變化:

int maxNumber, num1, num2, sum, answer, problems, i; 

要:

int maxNumber = 0, num1 = 0, num2 = 0, sum = 0, answer = 0, problems = 0, i = 0; 

此外,移動sum = num1 + num2;線後,您創建num1num2

+0

這些初始化是不需要的(除了'i'),因爲它們要麼被計算出來要麼作爲輸入 –

+0

它似乎沒有初始化就可以正常工作,但是我會改變它 –

+0

將變量初始化爲已知值。@Leopold_Stotch:'i' DEFIINITELY需要初始化 – rost0031

0

您的程序有多個問題,包括格式。這裏是更正:

#include <stdio.h> 
#include <time.h> 
#include <stdlib.h> 

int main(){ 
    int maxNumber, num1, num2, sum, answer, problems, i; 

    srand(time(NULL)); 

    //printf("Would you like 1)Addition or 2)Multiplication?\n") 
    printf("Enter your Max Number:"); 
    scanf("%d", &maxNumber); 

    printf("How many problems do you want?\n"); 
    scanf("%d", &problems); 

    // issue: i was not initialized 
    i = 0; 
    while(i != problems){ 
     i++; 
     num1 = rand()%maxNumber; 
     num2 = rand()%maxNumber; 

     printf("What is %d + %d\n", num1, num2); 
     // issue: sum was not calculated here 
     sum = num1 + num2; 
     scanf("%d", &answer); 

     if(answer != sum){ 
      printf("Sorry, that's incorrect, the answer is %d\n", sum); 
     } 
     else{ 
      printf("Correct!\n"); 
     } 
    } 
    return 0; 
} 
+0

謝謝Serge,以及其他人。非常感謝! –

0

您定義sum = num1 + num2,其中num1和num2根本沒有分配。 C不會將數字默認初始化爲零。這就是爲什麼你有這麼大的數字。

只需在num2 = rand()%maxNumber;之後加sum = num1 + num2,一切都應該有效!