需要一个不满足的 if 语句来转到代码开头

我正在尝试设置一些 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 语句说它是一个错误,它应该返回要求他们重新输入天数


慕斯709654
浏览 140回答 4
4回答

冉冉说

使用do-while至少执行一次的循环,如果days不满足条件则每次都循环回来。public static void main(String[] args)&nbsp;{&nbsp; &nbsp; do {&nbsp; &nbsp; &nbsp; &nbsp; //Your other code&nbsp; &nbsp; &nbsp; &nbsp; String animal = JOptionPane.showInputDialog("Enter In Animal Name"); // Asking for user to enter a animal&nbsp; &nbsp; &nbsp; &nbsp; String fruit = JOptionPane.showInputDialog("Enter In A Fruit Name"); // Asking user to enter a fruit&nbsp; &nbsp; &nbsp; &nbsp; int days = askForInput();&nbsp; &nbsp; &nbsp; &nbsp; if (days <= 0 || days >= 10) { // Ensures that negative numbers cannot be entered.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JOptionPane.showMessageDialog(null, "Error, Please enter a number between 1 and 10");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } while (days <= 0 || days >= 10);}//Pass whatever parameters you might needpublic static int askForInput() {&nbsp; &nbsp; int days = Integer.parseInt(JOptionPane.showInputDialog("Enter In How Many Days Between 1 And 10"));&nbsp; &nbsp; //Any other code you want&nbsp; &nbsp; return days;}我还将它提取到一个方法中,这可能是不必要的,但如果需要,它可以让您添加更多功能。如果您不想每次都被问到这个问题,您也可以移动animal或fruit离开。do

哆啦的时光机

允许代码重新启动代码块(即返回到开头并重试)的一种方法是使用带有“永远”循环的continueand语句。breakfor (;;) { // loop forever&nbsp; &nbsp; // some code here&nbsp; &nbsp; if (failure condition 1) {&nbsp; &nbsp; &nbsp; &nbsp; // handle failure here&nbsp; &nbsp; &nbsp; &nbsp; continue; // go back and try again&nbsp; &nbsp; }&nbsp; &nbsp; if (failure condition 2) {&nbsp; &nbsp; &nbsp; &nbsp; // handle failure here&nbsp; &nbsp; &nbsp; &nbsp; continue; // go back and try again&nbsp; &nbsp; }&nbsp; &nbsp; // more code and failure condition checks here&nbsp; &nbsp; break; // unconditional exit loop, since all is ok}如果“此处的某些代码”本身位于循环内,但需要返回到开头并重试,则可以为此使用标签:TRYAGAIN: for (;;) { // loop forever&nbsp; &nbsp; // some code here&nbsp; &nbsp; for (some looping here) {&nbsp; &nbsp; &nbsp; &nbsp; // some code here&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (failure condition) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // handle failure here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue TRYAGAIN; // go back and try again&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } finally {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // code here will execute, even if 'continue' is used&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // more code and failure condition checks here&nbsp; &nbsp; break; // unconditional exit loop, since all is ok}

吃鸡游戏

String animal;String fruit;int days = 0;animal = JOptionPane.showInputDialog("Enter In Animal Name");fruit = JOptionPane.showInputDialog("Enter In A Fruit Name");while(days <= 0 || days > 10) {&nbsp; &nbsp; days = Integer.parseInt(JOptionPane.showInputDialog("Enter In How Many Days Between 1 And 10"));&nbsp; &nbsp; if (days <= 0 || days > 10) {&nbsp; &nbsp; &nbsp; &nbsp; JOptionPane.showMessageDialog(null, "Error, Please enter a number between 1 and 10");&nbsp; &nbsp; }}

临摹微笑

使用 while 循环// Your user prompt for animal and fruit goes hereboolean exit = false;while(!exit){&nbsp; &nbsp; // Your user prompt for days code goes here&nbsp; &nbsp; if (days <= 0 || days > 10) {&nbsp; &nbsp; &nbsp; &nbsp; JOptionPane.showMessageDialog(null, "Error, Please enter a number between 1 and 10");&nbsp; &nbsp; &nbsp; &nbsp; exit = false; // This is not necessary but nice for readability's sake&nbsp; &nbsp; }&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; exit = true;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java