英文:
Why can't I check if a method returns true in my If statement?
问题
我尝试实现的目标是要使我的if语句检查我的verifyAnswer方法是否返回true,如果返回true,我希望它取消我的计时器。所以我查找了如何做以下操作的方法,但我找到的答案都说类似的事情,然而,我认为由于方法中的参数依赖于用户答案,所以它不起作用。这似乎很简单,但我是Java的新手,似乎无法让它工作。谢谢大家的帮助!
public boolean verifyAnswer(String userAnswer) {
String correctAnswer = this.questions.get(currentQuestionIndex).correctAnswerText;
if(userAnswer.equals(correctAnswer)) {
timer.pauseTimer();
JOptionPane.showMessageDialog(null, "Correct Answer");
return true;
}
else {
timer.pauseTimer();
JOptionPane.showMessageDialog(null, "Wrong Answer");
return false;
}
}
Timer t = new Timer();
int[] score = {0};
TimerTask tt = new TimerTask() {
@Override
public void run() {
System.out.println(++score[0]);
if (score[0] == 30) {
t.cancel();
}
else if(verifyAnswer()) { //为什么这行不起作用?
t.cancel();
}
}
};
t.scheduleAtFixedRate(tt, 0, 1000);
以下是一个带有动作监听器的标签,因此当用户点击它时,它会检查标签中的文本与verifyAnswer方法中的文本是否匹配,以查看用户是否选择正确。
label_option_a = new JLabel("<html>Option A</html>");
label_option_a.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
verifyAnswer(label_option_a.getText());
}
});
英文:
What I am trying to achieve is that I want my if statement to check if my method verifyAnswer returns true, and if it does I want it to cancel my timer. So I searched up how to do the following but the answer I found all said similar things, however I think that since my argument in the method depends on a user answer it isn't working. This seems simple but I am new to Java and can't seem to make it work. Thank you all for the help!
public boolean verifyAnswer(String userAnswer) {
String correctAnswer = this.questions.get(currentQuestionIndex).correctAnswerText;
if(userAnswer.equals(correctAnswer)) {
timer.pauseTimer();
JOptionPane.showMessageDialog(null, "Correct Answer");
return true;
}
else {
timer.pauseTimer();
JOptionPane.showMessageDialog(null, "Wrong Answer");
return false;
}
}
Timer t = new Timer();
int[] score = {0};
TimerTask tt = new TimerTask() {
@Override
public void run() {
System.out.println(++score[0]);
if (score[0] == 30) {
t.cancel();
}
else if(verifyAnswer()) { //Why doesn't this line work?
t.cancel();
}
};
};
t.scheduleAtFixedRate(tt, 0, 1000);
Below is a label with an action listener, so when the user clicks on it, it checks the text that is in the label with the verifyAnswer method to see if the user chose correctly.
label_option_a = new JLabel("<html>Option A</html>");
label_option_a.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
verifyAnswer(label_option_a.getText());
}
});
答案1
得分: 1
因为您没有在verifyAnswer
方法中传递参数。verifyAnswer
需要一个字符串参数。
您应该像这样调用:
else if (verifyAnswer("传递给userAnswer的参数")) {
t.cancel();
}
英文:
Because you are not passing an argument in verifyAnswer
method. verifyAnswer
expect string parameter.
public boolean verifyAnswer(String userAnswer) { .. }
You should call like this
else if(verifyAnswer("pass_what_argument_you_want_to_pass_for_user_answer")) {
t.cancel();
}
答案2
得分: -1
你应该更改你方法中的true和false返回值。像这样:
public boolean verifyAnswer(String userAnswer) {
String correctAnswer = this.questions.get(currentQuestionIndex).correctAnswerText;
if (userAnswer.equals(correctAnswer)) {
timer.pauseTimer();
JOptionPane.showMessageDialog(null, "正确答案");
return false;
} else {
timer.pauseTimer();
JOptionPane.showMessageDialog(null, "错误答案");
return true;
}
}
英文:
You should change true and false returns in your method. Like that
public boolean verifyAnswer(String userAnswer) {
String correctAnswer = this.questions.get(currentQuestionIndex).correctAnswerText;
if(userAnswer.equals(correctAnswer)) {
timer.pauseTimer();
JOptionPane.showMessageDialog(null, "Correct Answer");
return false;
}
else {
timer.pauseTimer();
JOptionPane.showMessageDialog(null, "Wrong Answer");
return true;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论