我正在尝试设置一些 if 语句,以确保用户输入不会超出我设置的范围(如果我希望 if 语句将它们返回到主代码的开头)。
String animal = JOptionPane.showInputDialog("Enter In Animal Name"); // Asking for user to enter a animal
String fruit = JOptionPane.showInputDialog("Enter In A Fruit Name"); // Asking user to enter a fruit
int days = Integer.parseInt(JOptionPane.showInputDialog("Enter In How Many Days Between 1 And 10"));
// Above integer is asking the user to enter the amount of days.
// Below if statements are basically error checking to ensure the user stays between the
// range asked for when they are asked to enter in days between 1 and 10.
if (days <= 0) { // Ensures that negative numbers cannot be entered.
JOptionPane.showMessageDialog(null, "Error, Please enter a number between 1 and 10");
return;
}
if (days >= 10) { // Ensures nothing over 10 can be entered.
JOptionPane.showMessageDialog(null, "Error, Please enter a number between 1 and 10");
return;
}
如果 if 语句说它是一个错误,它应该返回要求他们重新输入天数
冉冉说
哆啦的时光机
吃鸡游戏
临摹微笑
相关分类