【发布时间】:2020-04-18 08:24:14
【问题描述】:
我正在尝试生成随机运算符以使随机问题具有随机运算符,请帮助
public void generateNewAddQuestion () {
Random rand = new Random();
int a = rand.nextInt(21);
int b = rand.nextInt(21);
int c =0;
sumTextView.setText(Integer.toString(a) + "+" + Integer.toString(b));
locationOfCorrecAnswer = rand.nextInt(4);
answers.clear();
String[] operators = {"+", "-", "x", "/"};
int incorrectAnswer;
for (int i=0; i < 4; i++) {
if (i == locationOfCorrecAnswer) {
answers.add(a+b);
sumTextView.setText(Integer.toString(a)+"+"+Integer.toString(b));
}
else {
incorrectAnswer = rand.nextInt(41);
while(incorrectAnswer == a+b) {
incorrectAnswer = rand.nextInt(41);
}
answers.add(incorrectAnswer);
}
}
button2.setText(Integer.toString(answers.get(0)));
button3.setText(Integer.toString(answers.get(1)));
button4.setText(Integer.toString(answers.get(2)));
button5.setText(Integer.toString(answers.get(3)));
}
【问题讨论】:
-
我还有四个用于用户界面的按钮,我只需要一种使操作员更加动态的方法,目前,添加工作正常
-
在有人完全不理我并说以前有人问过这个问题之前,我已经完成了我的研究,从我的角度来看,我刚刚检查的那些与这个不相似,提前谢谢
-
与其防御,不如列出相似但不重复的问题。