I am trying to set some if-statements to ensure user inputs do not go out of ranges I have set if they do I want the if statement to return them to the beginning of the main code.
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 the if statement says that its an error it should go back to asking them to re enter the days
Aucun commentaire:
Enregistrer un commentaire