下面的代碼接受一個整數,並檢查和打印如果< 5,或者如果可以整除5,或者如果可以整除3,或者如果3和5可以整除。但是我想把代碼放入無限循環,以便控制檯重複要求我在打印輸出後輸入一個整數。這是我到目前爲止的代碼:無限循環的Java程序
import java.util.Scanner;
public class Q3c {
public static void main(String args[]) {
System.out.println("Enter an integer ");
Scanner input = new Scanner(System.in);
int n = input.nextInt();
if ((n%5 == 0) && (n%3 == 0)) {
System.out.println("The number " + n + " is divisible by 3 and 5");
}
else {
if(n%5 == 0) {
System.out.println(n + " is divisble by 5");
}
if(n%3 == 0) {
System.out.println(n + " is divisble by 3");
}
}
if (n < 5) {
System.out.println(n + "is <5");
}
input.close();
}
}
演示輸出:
Enter an integer 5
5 is divisibe by 5
Enter an integer