I am coding a problem where it will be x (+ or - or *) y =z and it will generate 4 possible answers for the user which has 1 good and 3 wrong answers. I made most of the code but I do not get how I make the same formula used again for Reponse() because right now when I execute the code, Equation() makes his own one and Reponse() does another different formula. Also I need to know how I can make sure that the code works by adding a system that will show a formula like 5 +5 = ? and the code will show 4 answers which has one good one.
我正在编码一个问题,它将是x(+或 - 或*)y = z,它将为用户生成4个可能的答案,其中有1个好的和3个错误的答案。我制作了大部分代码,但是我没有得到如何为Reponse()再次使用相同的公式,因为现在当我执行代码时,Equation()使用自己的代码,而Reponse()执行另一个不同的公式。另外我需要知道如何通过添加一个显示类似5 +5 =的公式的系统来确保代码的工作原理?并且代码将显示4个答案,其中一个答案很好。
here's the code:
这是代码:
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);
}
}
and for Reponse() the one that generates the answers:
对于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 = '?';
}
}
}
1 个解决方案
#1
1
This is because you are initializing the new instance of Equation
class inside your Response
class.
这是因为您正在Response类中初始化Equation类的新实例。
Equation equ = new Equation();
Whenever you'll do something like,
每当你做某事时,
Response r = new Response();
A new instance of Equation
will be instantiated.
将实例化一个新的方程实例。
What you should be doing is as follows,
你应该做的是如下,
-
Change the
Response
class as follows:更改Response类,如下所示:
public class Response { int a, b, c, d; char operator = '?'; public Response(Equation equ) { 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 = '?'; } } }
Note: I have deleted the instance of Equation
from the class and am passing it to the constructor.
注意:我已从类中删除了Equation的实例,并将其传递给构造函数。
-
Create a new instance of
Equation
,创建一个新的Equation实例,
Equation equ = new Equation();
-
Create a new instance of
Response
by passing aboveEquation
instance,通过传递上面的Equation实例创建一个新的Response实例,
Response r = new Response(equ);
Now, you can create multiple instances of Response
class using the same instance of the Equation
class that you instantiated.
现在,您可以使用实例化的Equation类的相同实例创建Response类的多个实例。
#1
1
This is because you are initializing the new instance of Equation
class inside your Response
class.
这是因为您正在Response类中初始化Equation类的新实例。
Equation equ = new Equation();
Whenever you'll do something like,
每当你做某事时,
Response r = new Response();
A new instance of Equation
will be instantiated.
将实例化一个新的方程实例。
What you should be doing is as follows,
你应该做的是如下,
-
Change the
Response
class as follows:更改Response类,如下所示:
public class Response { int a, b, c, d; char operator = '?'; public Response(Equation equ) { 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 = '?'; } } }
Note: I have deleted the instance of Equation
from the class and am passing it to the constructor.
注意:我已从类中删除了Equation的实例,并将其传递给构造函数。
-
Create a new instance of
Equation
,创建一个新的Equation实例,
Equation equ = new Equation();
-
Create a new instance of
Response
by passing aboveEquation
instance,通过传递上面的Equation实例创建一个新的Response实例,
Response r = new Response(equ);
Now, you can create multiple instances of Response
class using the same instance of the Equation
class that you instantiated.
现在,您可以使用实例化的Equation类的相同实例创建Response类的多个实例。