java获取范围内的素数和总素数
我是Java的初学者。我写这个程序是为了显示用户提供的数字之间的所有素数
电流输出为:
2, 3, 5, 7, Count: 4
但是,我希望输出是这样的:
"The number of prime is: "+count+", and they are: " followed by all the numbers separated by comma
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);
}
}
# 1 楼答案
将素数存储在一个数组中,并在循环外的末端显示它们
# 2 楼答案
获取范围内的素数和总素数
输出:
输入限制:
十
从1到10的素数
12357
给定范围内的总质数:5
# 3 楼答案
必须存储这些值,例如:
# 4 楼答案
将生成的素数保存到列表中,最后生成输出。例如: