英文:
While loop nested if else java
问题
public class QuizW
{
    public static void main(String[] args)
    {
        String question = "What is the capital of Australia? \n";
        question += "A. Sydney\n";
        question += "B. Melbourne\n";
        question += "C. Perth\n";
        question += "D. Canberra\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);
        }
    }
}
英文:
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
得分: 1
你可以这样做:
public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    String question = "澳大利亚的首都是哪里? \n"
            + "A. 悉尼\n"
            + "B. 墨尔本\n"
            + "C. 珀斯\n"
            + "D. 堪培拉\n"
            + "E. 布里斯班\n";
    System.out.println(question);
    String answer = scanner.next().toUpperCase();
    while (true) {
        if (answer.equals("D")) {
            System.out.println("正确");
            break;
        } else if (answer.equals("A") || answer.equals("B") || answer.equals("C") || answer.equals("E")) {
            System.out.println("回答错误,请重试。");
        } else {
            System.out.println("输入错误");
        }
        answer = scanner.next().toUpperCase();
    }
}
简化检查错误答案的部分,你可以使用:
String[] wrong = {"A", "B", "C", "E"};
...
Arrays.asList(wrong).contains(answer);
英文:
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
<hr>
To make checking worng values shorter You can use:
String[] wrong = {"A", "B", "C", "E"};
...
Arrays.asList(wrong).contains(answer)
答案2
得分: 0
如果你在while循环开始时就要求答案,你只需要写一次:
String answer = null;
while (true) {
    answer = scanner.next().toUpperCase();
    if (answer.equals("D")) {
        System.out.println("正确");
        break;
    } else if (answer.equals("A") || answer.equals("B") || answer.equals("C") || answer.equals("E")) {
        System.out.println("回答错误,请重试。");
    } else {
        System.out.println("输入有误");
    }
}
这也解决了你的错误 ![]()
英文:
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 ![]()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论