【发布时间】:2020-08-01 22:30:04
【问题描述】:
所以,我试图从计时器任务中访问我的变量,但是我似乎无法让它工作。我阅读了有关全局变量的信息,但不太确定如何使用它。我是 Java 新手,所以任何建议都会非常有帮助,谢谢!
public boolean verifyAnswer(String userAnswer) {
String correctAnswer = this.questions.get(currentQuestionIndex).correctAnswerText;
if(userAnswer.equals(correctAnswer)) {
timer.pauseTimer();
Timer t = new Timer();
TimerTask tt = new TimerTask() {
//This is the variable I want to use
int score = 0;
@Override
public void run() {
System.out.println(++score);
if (score == 30) {
t.cancel();
}
};
};
t.scheduleAtFixedRate(tt, 0, 1000);
TimerPanel timer2 = new TimerPanel();
long total = 0;
//Here is where I try to use it
long equation = TimerTask.score / 30000;
【问题讨论】:
标签: java variables global-variables local-variables