当循环嵌套条件分支 Java

huangapple go评论66阅读模式
英文:

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 = {&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;E&quot;};
...
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("输入有误");
    }
}

这也解决了你的错误 当循环嵌套条件分支 Java

英文:

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(&quot;D&quot;)) {
        System.out.println(&quot;Correct&quot;);
        break;
    } else if (answer.equals(&quot;A&quot;) || answer.equals(&quot;B&quot;) || answer.equals(&quot;C&quot;) || answer.equals(&quot;E&quot;)) {
        System.out.println(&quot;Wrong answer, try again.&quot;);
    } else {
        System.out.println(&quot;Incorrect input&quot;);
    }
}

And it also solves your bug 当循环嵌套条件分支 Java

huangapple
  • 本文由 发表于 2020年9月15日 21:59:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63903530.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定