我试图随机生成一个方程式,该方程式也有50%的错误机会,并显示错误的答案。不正确的答案应该有-2,-1,+ 1或+2的错误。
有时我的代码打印出这样的分割方程式(我无法发布图像): 2/10 = 13 1/5 = 43 等
我无法弄清楚为什么等式显示未经检查的数字组合?
(首先在我的 onCreateView 方法中调用 generateNumbers()
public void generateNumbers() {
//randomly generate 2 numbers and an operator
number1 = (int) (Math.random() * 10) + 1;
number2 = (int) (Math.random() * 10) + 1;
operator = (int) (Math.random() * 4) + 1;
//50% chance whether the displayed answer will be right or wrong
rightOrWrong = (int) (Math.random() * 2) + 1;
//calculate the offset of displayed answer for a wrong equation (Error)
error = (int) (Math.random() * 4) + 1;
generateEquation();
}
public void generateEquation() {
StringBuilder equation = new StringBuilder();
//append the first number
equation.append(number1);
//generate/append the operator and calculate the real answer
if (operator == 1) {
equation.append(" + ");
actualAnswer = number1 + number2;
} else if (operator == 2) {
equation.append(" - ");
actualAnswer = number1 - number2;
} else if (operator == 3) {
equation.append(" x ");
actualAnswer = number1 * number2;
} else if (operator == 4) {
if ((number1%number2==0) && (number1>number2)) {
actualAnswer = number1 / number2;
} else {
generateNumbers();
}
equation.append(" / ");
}
//append the second number and the equals sign
equation.append(number2 + " = ");
//we will display the correct answer for the equation
if (rightOrWrong == 1) {
displayedAnswer = actualAnswer;
equation.append(displayedAnswer);
}
//we will display an incorrect answer for the equation
//need to calculate error (-2, -1, +1, +2)
else {
if (error == 1) {
displayedAnswer = actualAnswer - 1;
} else if (error == 2) {
displayedAnswer = actualAnswer - 2;
}else if (error == 3) {
displayedAnswer = actualAnswer + 1;
}else {
displayedAnswer = actualAnswer + 2;
}
//append the displayed answer with error
equation.append(displayedAnswer);
}
questionNumber.setText("You have answered " + count + " out of 20 questions");
finalEquation.setText(equation.toString());
}
答案 0 :(得分:5)
我认为您需要在调用return
之后添加generateNumbers
声明
} else if (operator == 4) {
if ((number1%number2==0) && (number1>number2)) {
actualAnswer = number1 / number2;
} else {
generateNumbers();
}
equation.append(" / ");
}
因为这将重新启动整个过程,而不是继续使用更多数字。
答案 1 :(得分:0)
更改您的代码以解除低机会分裂问题:
public void generateNumbers() {
number1 = (int) (Math.random() * 10) + 1;
number2 = (int) (Math.random() * 10) + 1;
//don't get operator here
//operator = (int) (Math.random() * 4) + 1;
rightOrWrong = (int) (Math.random() * 2) + 1;
error = (int) (Math.random() * 4) + 1;
//don't generate equation here, start (in the View with generateEquation instead of generate numbers
//generateEquation();
}
public void generateEquation() {
StringBuilder equation = new StringBuilder();
generateNumbers();
//determine operator here
operator = (int) (Math.random() * 4) + 1;
equation.append(number1);
if (operator == 1) {
equation.append(" + ");
actualAnswer = number1 + number2;
} else if (operator == 2) {
equation.append(" - ");
actualAnswer = number1 - number2;
} else if (operator == 3) {
equation.append(" x ");
actualAnswer = number1 * number2;
} else if (operator == 4) {
equation.append(" / ");
// generate new numbers if they are not suiteable
while((number1%number2!=0) && (number1<number2))
{
generateNumbers();
}
actualAnswer = number1 / number2;
}
......