当用户选择除 1 或 2 以外的其他内容时如何打印消息

How to print a message when the user selects something else except 1 or 2

我想要一个 if 语句,它不会让用户 select 其他东西并打印一条错误消息

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

您需要像这样使用 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");
                }
            }
        }