我是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);
}
}
答案 0 :(得分:3)
您必须存储值,例如:
import java.util.ArrayList;
import java.util.List;
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();
List<Integer> primes = new ArrayList<>();
// 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) {
primes.add(i);
}
}
System.out.println("The number of prime is: " + primes.size() + ", and they are: " + primes.toString());
}
}
答案 1 :(得分:3)
谢谢大家帮忙。
我的最终代码:
package com.example.test;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class PrimeTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number till which the prime numbers are to be calculated: ");
int input = scanner.nextInt();
scanner.close();
List<Integer> primes = new ArrayList<>();
// loop through the numbers one by one (edit include input in range)
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) {
primes.add(i);
}
}
String s = primes.toString().replace("[", "").replace("]", "");
System.out.println("The number of prime is: " + primes.size() + ", and they are: " + s);
}
}
输出:
Enter the number till which the prime numbers are to be calculated: 70
The number of prime is: 19, and they are: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67
答案 2 :(得分:1)
将素数存储在一个数组中,并在循环外的末尾显示它们。
答案 3 :(得分:1)
将生成的素数保存到List中并最终生成输出。例如:
System.out.println("The number of prime is: " + list.size());
foreach(int prime : list)
System.out.print(prime + ", ");
答案 4 :(得分:1)
不是打印新找到的素数(System.out.print(i + ", ");
),而是将其存储在列表(primes.add(i)
)中,并在程序结束时显示此列表。您的列表必须在for
循环(List<Integer> primes = new ArrayList<Integer>()
;)之外宣布。
答案 5 :(得分:0)
获取范围内的素数和总素数
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ListPrimeNumbers {
/**
* @param args
* @throws IOException
* @throws NumberFormatException
*/
public static void main(String[] args) throws NumberFormatException, IOException {
int count=0;
int limit;
System.out.println("Enter the Limit:");
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(read);
limit = Integer.parseInt(in.readLine());
System.out.println("Prime numbers From 1 to " + limit);
//loop from 1 to limit
for(int i=1; i < limit; i++) {
boolean isPrime = true;
//check to see if the number is prime
for(int j=2; j < i ; j++) {
if(i % j == 0) {
isPrime = false;
break;
}
}
// print the number
if(isPrime) {
System.out.print(i + " ");
count++;
}
}
System.out.println("\nTotal Prime Number in given range: "+count);
}
}
<强>输出:强>
输入限制:
10
素数从1到10
1 2 3 5 7
给定范围内的总素数:5
答案 6 :(得分:0)
我写了一个列出并计算1和任何给定数字之间的素数而不使用任何布尔值。可能你觉得很有趣。
public class Main {
public static void main(String[] args) {
int limit = 30; // The program will show prime numbers between 1 and this limit.
System.out.println("Prime numbers between 1 and "+limit+" (except the number 2) :");
System.out.println();
int amount=1; // The amount will give how many prime numbers exist between the interval.
for (int dividend =2; dividend <=limit; dividend++) {
for (int divider=2; divider< dividend; divider++) {
int remaining = dividend % divider;
if (remaining==0){
break;
}
if (divider==dividend-1) {
// If the loop manages to come here without having broken, then the dividend a prime number.
System.out.println(dividend + " IS A PRIME NUMBER.");
amount++;
}
}
}
System.out.println();
System.out.println("There are a total of "+amount+" prime numbers between 1 and "+limit+" including the number 2. ");
}
}
答案 7 :(得分:0)
public static void main(String[] args)
{
int i,high=10 , low=2,flag ,count=0 ;
while (low<high)
{
flag=0;
for (i=2;i<=low/2;i++)
{
if (low%i==0)
{
flag=1;
break;
}
}
if (flag==0)
{
System.out.println(low);
count++;
}
++low;
}
System.out.println("Total number are: "+count);
}
}
答案 8 :(得分:0)
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class PrimeNumbersInRange {
public static void main(String[] args) {
int lb = 0, ub = 20;
List<Integer> list = IntStream.rangeClosed(lb,ub)
.filter(PrimeNumbersInRange::isPrime)
.boxed()
.collect(Collectors.toList());
System.out.println(list);
System.out.println(list.size());
}
public static boolean isPrime(int num) {
if (num < 2 || (num > 2 && num % 2 == 0)) {
return false;
}
for (int i = 3; i <= Math.sqrt(num); i++) {
if (num % i == 0)
return false;
}
return true;
}
}