微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

While循环是否嵌套Java

如何解决While循环是否嵌套Java

我的while循环有问题。它需要不断提出问题,直到答案正确为止。有人可以建议吗?

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

解决方法

您可以执行以下操作:

SELECT A.username,B.user_country,D.firstname shiping_f_name,D.address shiping_address,E.firstname billing_f_name,E.address billing_address
FROM Our_user A
INNER JOIN Our_user_ext B ON A.userid = B.userid
INNER JOIN Our_user_address C  ON A.userid = C.userid
INNER JOIN Our_user_contact_info D ON C.shippingaddressid= D.shippingaddressid
INNER JOIN Our_user_contact_info E ON C.billingaddressid = E.billingaddressid

我仅使用控制台,没有JPannels,但是您可以理解逻辑

结果控制台:

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

要缩短检查损耗值,可以使用:

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
,

如果您在一开始就要求答案,则只需编写一次即可:

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

它也解决了您的错误:)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。