为什么我不能在我的If语句中检查一个方法是否返回true?

huangapple go评论102阅读模式
英文:

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, &quot;Correct Answer&quot;);
		return true;
	}
	else {
		timer.pauseTimer();
		JOptionPane.showMessageDialog(null, &quot;Wrong Answer&quot;);
		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&#39;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(&quot;&lt;html&gt;Option A&lt;/html&gt;&quot;);
	    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(&quot;pass_what_argument_you_want_to_pass_for_user_answer&quot;)) { 
     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, &quot;Correct Answer&quot;);
    return false;
}
else {
    timer.pauseTimer();
    JOptionPane.showMessageDialog(null, &quot;Wrong Answer&quot;);
    return true;
}

} 

huangapple
  • 本文由 发表于 2020年8月2日 14:08:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63212926.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定