【发布时间】:2018-02-26 19:29:48
【问题描述】:
我正在编写一个问题,它将是 x(+ 或 - 或 *)y =z,它将为用户生成 4 个可能的答案,其中有 1 个好答案和 3 个错误答案。我编写了大部分代码,但我不知道如何为 Reponse() 再次使用相同的公式,因为现在当我执行代码时,Equation() 自己创建一个,而 Reponse() 使用另一个不同的公式。另外我需要知道如何通过添加一个显示公式的系统来确保代码正常工作 5 +5 = ? 并且代码将显示 4 个答案,其中有一个好的答案。
代码如下:
public class Equation {
int x, y, z;
public Equation() {
Random r = new Random();
x = r.nextInt(50) + 1;
y = r.nextInt(50) + 1;
z = 0;
char operator = '?';
switch (r.nextInt(3)) {
case 0:
operator = '+';
z = x + y;
break;
case 1:
operator = '-';
z = x - y;
;
break;
case 2:
operator = '*';
z = x * y;
;
break;
default:
operator = '?';
}
System.out.print(x);
System.out.print(" ");
System.out.print(operator);
System.out.print(" ");
System.out.print(y);
System.out.print(" = ");
System.out.println(z);
}
}
对于 Reponse() 生成答案的那个:
public class Reponse {
Equation equ = new Equation();
int a, b, c, d;
char operator = '?';
public Reponse() {
Random r = new Random();
switch (r.nextInt(4)) {
case 0:
a = r.nextInt(2 * equ.z);
break;
case 1:
b = r.nextInt(2 * equ.z);
break;
case 2:
c = r.nextInt(2 * equ.z);
break;
case 3:
d = equ.z;
break;
default:
operator = '?';
}
}
}
【问题讨论】:
-
我对这个问题投了反对票,因为您在这里发布了大量代码,但没有说明它有什么问题。我们希望看到您期望代码做什么,为什么期望它这样做,它实际在做什么(在适当的地方带有完整的错误消息和堆栈跟踪),以及为什么它是错误的。请edit您的问题包含此信息,然后我会考虑撤回我的反对票。
-
@JoeC 我说 Equation 会生成公式,但 Reponse() 也会生成公式,而不是在控制台中生成 4 个答案选项,询问哪个是正确的
标签: java math random java.util.scanner equation