我們必須制定計劃,而不是針對不同的年級生成數學問題,因此它們會有所不同。我們必須在最小和最大邊界內產生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;
}
}
你在void方法'generateQuestion()'中使用變量'result&op'的地方? – 2014-09-22 05:02:45
我使用操作時,我從getOper方法切換我的操作,並將數字年級別號碼轉換爲操作,例如'+'' - '等。然後我使用結果,然後我打破每個案例將其轉換爲字符串 – lildizzle63 2014-09-22 05:23:24
你的預期產出是多少?你提到一個圖像,但我看不到一個 – Constantinos 2014-09-22 05:54:35