1

I have a problem with my while loop. It needs to keep asking the question until the answer is correct. Can someone please advise?

public class QuizW
{
    public static void main(String[] args)
    {
        String question = "What is the capital of Australia? \n";
        question += "A. Sydney\n";
        question += "B. Melburne\n";
        question += "C. Perth\n";
        question += "D. Cannberra\n";
        question += "E. Brisbane\n";

        String answer = JOptionPane.showInputDialog(question);
        answer = answer.toUpperCase();
        int answerCorrect = 0;
        
        while (answerCorrect == 0)
        {
            if (answer.equals("D"))
            {
                JOptionPane.showMessageDialog(null, "Correct!");
                break;
            }
            else if (answer.equals("A") || answer.equals("B") || answer.equals("C") || answer.equals("E"))
            {
                JOptionPane.showMessageDialog(null, "Incorrect. Please try again.");
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Invalid answer. Please enter A, B, C, D, or E.");
            }
            JOptionPane.showInputDialog(question);
        }
    }
}
1
  • 2
    you forgot update the answer inside the loop -> answer = JOptionPane.showInputDialog(question); Commented Sep 15, 2020 at 14:06

2 Answers 2

1

You can do it something like this:

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    String question = "What is the capital of Australia? \n"
            + "A. Sydney\n"
            + "B. Melbourne\n"
            + "C. Perth\n"
            + "D. Canberra\n"
            + "E. Brisbane\n";

    System.out.println(question);
    String answer = scanner.next().toUpperCase();

    while (true) {
        if (answer.equals("D")) {
            System.out.println("Correct");
            break;
        } else if (answer.equals("A") || answer.equals("B") || answer.equals("C") || answer.equals("E")) {
            System.out.println("Wrong answer, try again.");
        } else {
            System.out.println("Incorrect input");
        }
        answer = scanner.next().toUpperCase();
    }
}

I am using only console, without JPannels, but You can get the logic

Result console:

What is the capital of Australia? 
A. Sydney
B. Melburne
C. Perth
D. Cannberra
E. Brisbane

a
Wrong answer, try again.
b
Wrong answer, try again.
p
Incorrect input
d
Correct

To make checking worng values shorter You can use:

String[] wrong = {"A", "B", "C", "E"};
...
Arrays.asList(wrong).contains(answer)
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much. I am still missing something with the update answer = answer.toUpperCase(); JOptionPane.showInputDialog(question);
With an update after the wrong answer? If You want to display a question again You have to put it at the beginning of the while loop and don't use it before loop
Thank you so much. You saved me a couple of hours sleep. Your virtual beer or coffee is on me. It was so simple....
0

If you ask for the answer at the beginning of the while you only have to write it once:

String answer =null;
while (true) {
    answer = scanner.next().toUpperCase();
    if (answer.equals("D")) {
        System.out.println("Correct");
        break;
    } else if (answer.equals("A") || answer.equals("B") || answer.equals("C") || answer.equals("E")) {
        System.out.println("Wrong answer, try again.");
    } else {
        System.out.println("Incorrect input");
    }
}

And it also solves your bug :)

1 Comment

Made it. Thank you for your help

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.