samedi 1 août 2020

Why can't I check if a method returns true in my If statement?

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);

Aucun commentaire:

Enregistrer un commentaire