我是
Java的初學者.我正在編寫這個程式,以顯示使用者提供的數字之間的所有素數.
目前的輸出是:
2,3,5,7,Count: 4
但是,我希望輸出如下:
“素數的數量是:”count“,它們是:”後面跟逗号分隔的所有數字
package com.example.test;
import java.util.Scanner;
public class PrimeTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out
.println("Enter the number till which the prime numbers are to be calculated: ");
int input = scanner.nextInt();
int count = 0;
// loop through the numbers one by one
for (int i = 2; i < input; i++) {
boolean isPrimeNumber = true;
// check to see if the number is prime
for (int j = 2; j < i; j++) {
if (i % j == 0) {
isPrimeNumber = false;
break; // exit the inner for loop
}
}
// print the number if prime
if (isPrimeNumber) {
count++;
System.out.print(i + ",");
}
}
System.out.println("Count: " + count);
}
}