我正在做一个测验程序,要求用户简单的数学问题,接受答案,计算用户的分数等...如何将一个int值传递给JButton actionlistener?
我得到一个错误,因为我使用的是一个变量情况下,X)一个ActionListener内:
for(x = 0;x < total;x++){
System.out.print((x+1)+". ");
questionLabel.setText(number1" + "+ number2);
answerButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
int returnedAns = Integer.parseInt(answerTextField.getText());
if(returnedAns == answerToTheQuestion){
score++;
System.out.println("correct");
question[x].result = true;
}else{
System.out.println("wrong");
question[x].result = false;
}
try{
Thread.sleep(500);
}catch(Exception e){}
}
});
}
当我运行我的代码,它突出INT x和说,“从内部CLAS引用的局部变量必须是最后的或有效的决赛。”
请帮帮我我真的不知道应该怎么做。
我不能将其标记为最终我需要能够改变它的for循环工作...
使变量'final' :) – notyou
但事情是我需要去改变它....它的循环变量 –