2014-09-22 80 views
0

我們必須制定計劃,而不是針對不同的年級生成數學問題,因此它們會有所不同。我們必須在最小和最大邊界內產生2個隨機數。然後我們必須讓這兩個數字進行一次操作。例如將它們加在一起,或者一個除以另一個或者將它們相乘等。生成隨機數學問題

該程序基於用戶供應的年份級別工作。每年級別具有不同的最小和最大數量,以及不同的操作。例如。第1年只是加減法,第7年是加法減法乘法除法等。我定義了不同的方法來幫助我做到這一點,但我似乎無法讓我的程序生成我想要的。它應該看起來像下面附加的圖像。我的部分代碼就在這裏。當我運行我的程序時,它會產生一些整數(10或20,具體取決於用戶選擇嘗試多少個數學問題)。它不會產生數字之間的任何操作,例如(+, - ,/,x)

任何人都可以指出我正確的方向嗎?

private static void generateQuestion(int yearLevel) { 
    int min = getMin(yearLevel); 
    int max = getMax(yearLevel); 

    int num1 = (int) (min + (max - min) * Math.random()); 
    int num2 = (int) (min + (max - min) * Math.random()); 

    int oper = getOper(yearLevel); 
    String result = " "; 
    char op; 
    switch (oper) { 
     case 1: 
      op = '+'; 
      result = (num1 + num2 + " "); 
      break; 
     case 2: 
      op = '-'; 
      result = (num1 - num2 + " "); 
      break; 
     case 3: 
      op = '*'; 
      result = (num1 * num2 + " "); 
      break; 
     case 4: 
      op = '/'; 
      result = (num1/num2 + " "); 
      break; 
     case 5: 
      op = '%'; 
      result = (num1 % num2 + " "); 
      break; 

    } 
    ; 
} 

private static int getMin(int yearLevel) { 
    int min = 0; 
    if (yearLevel == 0 || yearLevel == 1 || yearLevel == 2 || yearLevel == 3 || yearLevel == 4) { 
     min = 0; 
    } 
    if (yearLevel == 5 || yearLevel == 6) { 
     min = -999; 
    } 
    if (yearLevel == 7) { 
     min = -9999; 
    } 

    return min; 
} 

private static int getMax(int yearLevel) { 

    int max = 9; 
    if (yearLevel == 0 || yearLevel == 1 || yearLevel == 2 || yearLevel == 3 || yearLevel == 4) { 
     max = 9; 
    } 

    if (yearLevel == 5 || yearLevel == 6) { 
     max = 999; 
    } 
    if (yearLevel == 7) { 
     max = 9999; 
    } 

    return max; 

} 

public static int getOper(int yearLevel) { 

    yearLevel = 0; 
    int opBounds = 1; 
    if (yearLevel == 1 || yearLevel == 2) { 
     opBounds = 2; 
    } 
    if (yearLevel == 3 || yearLevel == 4 || yearLevel == 5) { 
     opBounds = 4; 
    } 

    if (yearLevel == 7) { 
     opBounds = 5; 
    } 

    return opBounds; 

} 

}

+0

你在void方法'generateQuestion()'中使用變量'result&op'的地方? – 2014-09-22 05:02:45

+0

我使用操作時,我從getOper方法切換我的操作,並將數字年級別號碼轉換爲操作,例如'+'' - '等。然後我使用結果,然後我打破每個案例將其轉換爲字符串 – lildizzle63 2014-09-22 05:23:24

+0

你的預期產出是多少?你提到一個圖像,但我看不到一個 – Constantinos 2014-09-22 05:54:35

回答

0

我很喜歡你不少初學者,我會做更多的類問題舉行產生謎語和正確的結果。然後在您的

private static Question generateQuestion(int yearLevel) { 
    Question out=new Question(); 
    ... 
    //build string for selected case, for example 
    String answer=num1+"/"+num2; 
    double result=num1/num2; //rather use double 

    out.answer=answer; //String field 
    out.result=result; 

    return out; 
} 

然後通過顯示問題並將其與預期結果進行比較來進行處理。

0

你已經聲明結果爲一個字符串,並試圖在其中添加數字,你不能這樣做。
所以這個代碼:

String result = " "; 
    char op; 
    switch (oper) { 
     case 1: 
      op = '+'; 
      result = (num1 + num2 + " "); 
      break; 
     case 2: 
      op = '-'; 
      result = (num1 - num2 + " "); 
      break; 

更改爲類似:

int result = 0; 
    char op; 
    switch (oper) { 
     case 1: 
      op = '+'; 
      result = (num1 + num2); 
      break; 
     case 2: 
      op = '-'; 
      result = (num1 - num2); 
      break; 

然後,你將不得不使用Java方法,如 「Integer.toString(int i)以」 打印出來的整數結果(例)。