当用户选择除了1或2之外的其他内容时,如何打印信息[关闭] 。

问题描述 投票:-3回答:1

我想要一个if语句,它不会让用户选择其他东西,并打印一个错误信息。

while (!done) {

        String str1 = JOptionPane.showInputDialog("1:Student, 2:Graduate");
        if (str1.equals("1")) {
        //code
        } else if (str1.equals("2")) {
           //code`enter code here`
        } else if (str1.isEmpty()) {
            System.err.println("You have to choose between 1 and 2");
        }
}
java joptionpane
1个回答
-3
投票

你需要用 ifelse if 像这样

String str1 = JOptionPane.showInputDialog("1:Student, 2:Graduate");
                if (str1.equals("1")) {
                  System.out.println("Your chose 1");
        }

                else if (str1.equals("2")) {
                    System.out.println("Your chose 2");
        }

                else if (str1.isEmpty()) {
                    System.err.println("You have to choose between 1 and 2, your input is empty !");
        }

                else {
                    System.err.println("You have to choose between 1 and 2");
                }
            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.