我如何重复要求用户输入一个输入,直到用户输入负数。如果用户输入负数或者0,程序就会结束?
import java.util.Scanner;
public class OddEvenInt {
public static void main(String args[]) {
Scanner s = new Scanner(System. in );
int x;
do {
System.out.println("Enter an integer to check if it is odd or even ");
x = s.nextInt();
if (x % 2 == 0)
System.out.println("You entered an even number.");
else
System.out.println("You entered an odd number.");
} while (x % 2 == 0);
}
}
最佳答案
你必须更改 while 子句:
while (x>0)
关于java - java中奇数和偶数循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31726000/