由於某些原因,當我測試此值時,第二個if語句始終打印出1和1之間的每個數字將用戶輸入編號爲不是素數,即使它是。但是,第三條if語句正確指出用戶的數字是否爲素數。我在做什麼不對?試圖編寫詢問用戶號碼的代碼,確定它是否爲素數,並列出一個號碼與該號碼之間的素數
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in) ;
System.out.println("intput a number") ;
int number = input.nextInt() ;
int counter = 0 ;
int counter2 = 0 ;
for (int i = 1 ; i <= number ; i++) {
for (int j = 1 ; j <= i ; j++) {
if (i % j == 0) {
counter ++ ;
}
else if (i%j != 0) {
}
}
if (counter != 2) {
System.out.println(i+" is not prime") ;
}
if (counter == 2) {
System.out.println(i+", is a prime") ;
}
System.out.println("\n") ;
if (number % i == 0) {
counter2 ++ ;
}
}
if (counter2 != 2) {
System.out.println(number+" is not prime") ;
}
else if (counter2 == 2){
System.out.println(number+" is a prime") ;
}
}
* 「列出了數之間的素數」 *哪有之間的任何「 「一個號碼?你不需要兩個數字來表示「之間」有意義嗎? – Andreas