import java.util.Random;
class MathQuestion {
private int num1;
private int num2;
private int num3;
private String operator;
private int answer;
private boolean isCorrect = true;
public MathQuestion(int grade, int type) {
Random random = new Random();
if (grade == 1) {
if (type == 1)
{ // 10以内加减法:1步计算
this.num1 = random.nextInt(10);
this.num2 = random.nextInt(num1 + 1);
this.operator = random.nextInt(2) == 0 ? "+" : "-"; //使用 random.nextInt(2) 生成一个 0 或 1 的随机数,然后根据随机数来决定是加法还是减法。
}
else if (type == 2)
{ // 10以内加减法:2步计算 如:8-2-3,6-2+3
this.num1 = random.nextInt(10); // 第一步运算的第一个数
this.num2 = random.nextInt(num1 + 1); // 第一步运算的第二个数
this.num3 = random.nextInt(num2 + 1); // 第二步运算的第二个数
this.operator = "-";
}
else if (type == 3)
{ // 10-20以内的加减法:1步计算和2步计算
if (random.nextBoolean())
{ // 1步计算
this.num1 = random.nextInt(11) + 10;
this.num2 = random.nextInt(10);
}
else
{ // 2步计算
this.num1 = random.nextInt(11) + 10;
this.num2 = random.nextInt(num1-9) + 10;
int num3 = random.nextInt(num1-num2+1) + num2;
this.num2 = num3;
this.operator = "-";
}
}
else if (type == 4)
{ // 综合训练
int t = random.nextInt(3) + 1;
this.isCorrect = false; // 默认为错误答案,便于后面判断
if (t == 1)
{ // 10以内加减法:1步计算
this.num1 = random.nextInt(10);
this.num2 = random.nextInt(num1+1);
this.operator = random.nextBoolean() ? "+" : "-";
this.answer = this.operator.equals("+") ? num1 + num2 : num1 - num2;
}
else if (t == 2)
{ // 10以内加减法:2步计算
this.num1 = random.nextInt(10);
this.num2 = random.nextInt(num1+1);
int num3 = random.nextInt(num2+1);
this.num1 = this.num1 + num2;
this.num2 = num3;
this.operator = random.nextBoolean() ? "+" : "-";
this.answer = this.operator.equals("+") ? num1 + num2 + num3 : num1 - num2 - num3;
}
else if (t == 3)
{ // 10-20以内的加减法:1步计算和2步计算
if (random.nextBoolean()) { // 1步计算
this.num1 = random.nextInt(11) + 10;
this.num2 = random.nextInt(10);
this.operator = random.nextBoolean() ? "+" : "-";
this.answer = this.operator.equals("+") ? num1 + num2 : num1 - num2;
}
else
{ // 2步计算
this.num1 = random.nextInt(11) + 10;
this.num2 = random.nextInt(num1-9) + 10;
int num3 = random.nextInt(num1-num2+1) + num2;
this.num2 = num3;
this.operator = random.nextBoolean() ? "+" : "-";
this.answer = this.operator.equals("+") ? num1 + num2 + num3 : num1 - num2 - num3;
}
}
}
}
}
有懂Java的uu吗,这个2步计算该怎么弄啊,试了发现还是只显示一步计算。可以帮忙看看这段代码并修改吗?