2

I've been trying to create a program in Java that asks a question and then depending on the users's input displays one of the dialogs. For some reason, when I click the correct option, it gives me the "Incorrect. The correct answer is A." message, although it is supposed to say "Correct!". I suspect the mistake somewhere in the check() method (lines 25 to 34), however I am not that sure, as the code looks pretty simple and I can't really identify what is wrong and why the program doesn't work properly. Could anyone help?

import javax.swing.JOptionPane;
public class Quiz {

public static void main(String[] args) {
    String question = "What color is the hair of Disney princess Jasmine?\n";
    question += "A. Black\nB. White\nC. Green\nD. Blue\nE. Rainbow";
//  ask(question);
    check(question, "A");
}

static String ask(String question) {
    while (true) {
        String answer = JOptionPane.showInputDialog(question);
        answer = answer.toUpperCase();  

        if (answer.equals("A")) {
             return question.toUpperCase();
            }
        else if(!answer.equals("B") && !answer.equals("C") && !answer.equals("D") && !answer.equals("E")) {
             JOptionPane.showMessageDialog(null,"Invalid answer. Please enter A, B, C, D, or E.");  
        }
    }   
}

static void check(String question, String correctAnswer) {
    String answer = ask(question);  

    if (answer.equals(correctAnswer)) {
         JOptionPane.showMessageDialog(null,"Correct!");
    }
    else {
        JOptionPane.showMessageDialog(null, "Incorrect. The correct answer is A.");
    }
}

}

etaaaaaaa
  • 31
  • 4

3 Answers3

3

You have a small bug here,

if (answer.equals("A")) {
     return question.toUpperCase();
}

It should be

if (answer.equals("A")) {
     return "A";
}

See also check and

String answer = ask(question);
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
2

You should let the method "ask" return the answer itself and not the question.toUppercase.

Secondly, your method ask now only returns when the user did answer A. So you should make it in such way, that answers like B are also returned so that your "check" method (which seems correct), can classify these answers as wrong.

1

Your method ask(String question) considers the "A" option (the correct answer in your case) and all other possibilities except (B, C, D and E). Why? Because, in your if statement, you tell it to do so :

//This means in case we have all other answers except A (according to the 
//condition in  the if), B, C, D and E
else if(!answer.equals("B") && !answer.equals("C") && !answer.equals("D") && 
!answer.equals("E")) {
JOptionPane.showMessageDialog(null,"Invalid answer. Please enter A, B, C, D, 
or E.");  
}

You should add an else to complete it, and then cover all the options :

if (answer.equals("A")) {
//Case we choose A
return "A";
}else if(!answer.equals("B") && !answer.equals("C") && !answer.equals("D") && 
 !answer.equals("E")) {
 //All other options
 JOptionPane.showMessageDialog(null,"Invalid answer. Please enter A, B, C, D, 
 or E.");  
 }else{
  //Case we choose B, C, D or E
  //As the return is different from the correct answer
  //The message "Incorrect. The correct answer is A." should appear
  return "Incorrect answer";
  }