我想要一个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");
}
}
你需要用 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");
}
}
}