我正在做一个项目,其中所需的输出是:
Question 1: 12 + 0 = 12
Question 2: 6 + 12 = 18
Question 3: 3 + 8 = 11
Question 4: 11 - 0 = 11
Question 5: 8 + 7 = 15
Question 6: 8 - 3 = 5
Question 7: 8 - 2 = 6
Question 8: 12 - 9 = 3
Question 9: 0 + 10 = 10
Question 10: 4 + 5 = 9
Question 11: 2 + 1 = 3
Question 12: 1 + 12 = 13
Question 13: 12 + 0 = 12
Question 14: 10 - 1 = 9
Question 15: 11 - 8 = 3
其中操作数和操作符是随机化的。我需要在这个项目中使用两个不同的类,我有最大的困难与程序员定义的类称为问题,包含访问器方法和实例变量。
加法 -两个操作数,都在0-12范围内
减法 -两个操作数,第一个操作数在6-12的范围内,第二个操作数小于或等于第一个操作数。(这是为了避免负数)
生成一个随机生成加减法问题的类
规格:
3个实例变量: int operand1, int operand2, char operator
3 accessor方法: getOperand1, getOperand2, getOperator**
也是一个toString方法,返回一个包含问题(但不包含答案)的字符串,格式应为operand1、空格、操作符、空格、operand2、空格和一个等号。
到目前为止,我有这个:
package proj3;
public class Question {
private int operand1;
private int operand2;
private char operator;
public static int operand1(){
int num;
num = (int)(Math.random() * 13);
return num;
}
public static int operand2(){
int num;
num = (int)(Math.random() * 13);
return num;
}
public String toString(){
System.out.println(operand1 + operator + operand2 + "=");
return null;
}
}
我不确定是否我做了operand1方法正确与否,我也不确定如何去创建这个类的其余方法。任何帮助都是感激的。谢谢你。
每个Question
实例代表一个问题。因此,在创建实例时,应该在构造函数中生成随机值:
public Question() {
// generate the random values and store them in instance variables
}
- 首先生成操作符 根据操作符,可以生成操作数
- 将生成的值赋给实例变量
访问方法应该是实例方法,而不是静态方法,它们应该只返回相应的实例变量。
toString()
不应该打印任何东西-它应该只返回String
,而不是当前的null
。此外,您应该在字符串中添加一些空格,以便它具有所需的格式。
所以你的类看起来像这样:
public class Question {
private int operand1;
private int operand2;
private char operator;
public Question() {
// generate the random values and store them in instance variables
}
public int getOperand1() {
return operand1;
}
public int getOperand2() {
return operand2;
}
public char getOperator() {
return operator;
}
public String toString() {
return operand1 + " " + operator + " " + operand2 + " =";
}
}
如果您创建了Random
的实例,您可以使用更方便的方法来获取随机值:
Random r = new Random();
boolean randomBoolean = r.nextBoolean();
int randomInt = r.nextInt(13);
更新:
在你的尝试中,你做了
int num;
num = (int) (Math.random() * 2);
创建一个局部变量num
,并在0..1
范围内分配一个随机数。这个变量是构造函数的局部变量,它不存储在其他任何地方。您需要做的是为实例变量operand1
、operand2
和operator
赋值。
首先通过获得一个随机布尔值来决定操作符:
Random r = new Random();
if (r.nextBoolean()) {
// addition
} else {
// subtraction
}
然后在if
的每个分支中生成适当的随机值并将它们存储到实例变量中。同时存储操作符
例如:
要生成一个数字0..99
,您可以使用r.nextInt(100)
。
要生成一个数字80..99
,您可以使用r.nextInt(20) + 80
。
我将把最后的细节留给你:
public Question() {
Random r = new Random();
if (r.nextBoolean()) {
operator = '+';
operand1 = r.nextInt(13);
operand2 = // your code
} else {
operator = '-';
operand1 = // your code
operand2 = // your code
}
}
您可以这样测试输出:
Question q = new Question();
System.out.println("first operand: " + q.getOperand1());
System.out.println("full question: " + q); // toString() will be automatically used here
每个操作数可能需要2个方法,一个getter方法- getOperand1()
和一个generateOperand1()
(也就是上面所说的operand1()
)
对于随机生成,可以采用Enum参数typeOfOperation
,然后使用switch
语句生成相应的操作数。
。
void generateOperand1(OperationEnum typeOfOperation) {
switch (typeOfOperation) {
case OperationEnum.ADDITION
operand1 = // logic to generate random number
break;
case OperationEnum.SUBTRACTION
operand1 = // logic to generate random number
}
}
同样,您可以根据操作的类型创建一个generateOperator