Java 当用户选择除1或2之外的其他内容时,如何打印消息
我想要一个if语句,它不会让用户选择其他内容并打印错误消息Java 当用户选择除1或2之外的其他内容时,如何打印消息,java,joptionpane,Java,Joptionpane,我想要一个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`
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");
}
}
您需要与
if
和else 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");
}
}
}