嗨,伙计们,我正在编写一个数学测验程序作为学习练习,我无法让这个“响应”变量稍后在该方法中被识别。具体来说,两边带有 *s 的 'response' 变量不会链接到前面声明的响应变量。我是编程新手,所以很确定我犯了一个基本错误,但我无法解决,如果有人能帮助我,我将不胜感激。谢谢!
import acm.util.*;
import acm.program.*;
public class MathsQuiz extends ConsoleProgram{
public void run(){
println("This program gives atudents a series of maths questions");
askQuestions();
}
private void askQuestions(){
for (int i = 0; i < NUMBER_OF_QS; i++){
askQ();
}
}
private void askQ(){
int answer = rgen.nextInt(0,20);
int number1 = rgen.nextInt(0,20);
int number2 = answer - number1;
if (number2 > 0){
int response = readInt("What is " + number1 + "+" + number2 + "?");
}else {
int response = readInt("What is " + number1 + " " + number2 + "?");
}
if (**response** == answer){
println("Correct!");
}else{
println("Incorrect");
}
}
private RandomGenerator rgen = RandomGenerator.getInstance();
int NUMBER_OF_QS = 5;
int RES = 0;
}