简体   繁体   中英

While loop nested if else java

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

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM