我正在制作一个带有 while 循环的奇数或偶数程序。我想弄清楚如何以某个数字结束 while 循环。现在我有 1 来继续循环,并尝试使 2 成为终止循环的数字。还试图找出如果用户输入除数字(如字母/单词)以外的任何内容时如何终止程序。
package oddoreven;
import java.util.Scanner;
public class oddoreven {
public static void main (String[] args){
int num;
int x = 1;
while(x == 1) {
System.out.println("Enter a number to check whether or not it is odd or even");
Scanner s = new Scanner(System.in);
num = s.nextInt();
if (num % 2 == 0)
System.out.println("The number is even");
else
System.out.println("The number is odd");
//trying to figure out how to get the code to terminate if you put in a value that isn't a number
System.out.println("Type 1 to continue, 0 to terminate");
x = s.nextInt();
}
}
}
明月笑刀无情
繁星coding
慕尼黑5688855
繁花如伊
相关分类