我正在尝试创建一个简单的数学测验,该测验不能在问题中创建负数或对它们进行计数,但是我不确定我该如何处理。我不想列出一大堆问题,因为每个部分等都包含从0到15的每个数字,这毫无意义。
我的实际代码
public void setQuestion() {
Random rand = new Random();
int lhs = ThreadLocalRandom.current().nextInt(0,15);
int rhs = ThreadLocalRandom.current().nextInt(0,15);
int randomquestion = rand.nextInt(2);
if (randomquestion == 1) {
question.setText("What is " + lhs + " + " + rhs + "?");
expected = "" + (lhs + rhs);
} else if (randomquestion == 2 ) {
question.setText("What is " + lhs + " - " + rhs + "?");
expected = "" + (lhs - rhs);
} else {
question.setText("What is " + lhs + " * " + rhs + "?");
expected = "" + (lhs * rhs);
}
sumanswer.setText("You have got " + correctanswer +" CORRECT and " + wronganswer + " WRONG");
答案 0 :(得分:0)
这很简单,在其他情况下,如果为“-”,则您要求lhs必须大于rhs。 与此类似:
} else if (randomquestion == 2 ) {
if(lhs>rhs){
question.setText("What is " + lhs + " - " + rhs + "?");
expected = "" + (lhs - rhs);
}else{
question.setText("What is " + rhs + " - " + lhs + "?");
expected = "" + (rhs - lhs);
}
}
答案 1 :(得分:0)
如果我正确理解了这个问题,那么您不想在减法部分得到一个负数吗?您可以做一些简单的数学运算,以确保从较大的数中减去两个较小的数。试试这个:
question.setText("What is " + Math.Max(lhs, rhs) + " - " + Math.Min(lhs, rhs) + "?");
expected = "" + (Math.Max(lhs, rhs) - Math.Min(lhs, rhs));