我正在为我的班级开设一个程序,而且我似乎无法弄清楚如何循环它。所以我还没有完全摆脱循环,我想看一个例子,我将如何添加一个循环重复运行该程序,除非输入999
javax.swing.JOptionPane;
public class Evan {
public static void main (String[]args) {
String input1= JOptionPane.showInputDialog("Enter a evan number");
int num1 = Integer.parseInt( input1);
if (num1==999)
System.exit(num1);
if ( (num1%2) == 0 )
System.out.println("Good Job!");
else {
System.out.println("HEY thats not evan try again");
}
}
}
答案 0 :(得分:0)
围绕代码中应该与while (true)
重复的部分,这会让它永远继续下去(System.exit(num1)
仍将退出,但您应该用break;
替换它)
while (true) {
String input1= JOptionPane.showInputDialog("Enter a evan number");
int num1 = Integer.parseInt( input1);
if (num1==999)
System.exit(num1);
if ( (num1%2) == 0 )
System.out.println("Good Job!");
else {
System.out.println("HEY thats not evan try again");
}
}
答案 1 :(得分:0)
最好在适当的时候使用正确的条件。
Integer num1 = 0;
while (num1 != 999) {
String input1= JOptionPane.showInputDialog("Enter a evan number");
num1 = Integer.parseInt( input1);
if ( (num1%2) == 0 )
System.out.println("Good Job!");
else {
System.out.println("HEY thats not evan try again");
}
}
System.exit(num1);