有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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);
    }

}

共 (4) 个答案

  1. # 1 楼答案

    将素数存储在一个数组中,并在循环外的末端显示它们

  2. # 2 楼答案

    获取范围内的素数和总素数

    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);
        }
    
    }
    

    输出:

    输入限制:

    从1到10的素数

    12357

    给定范围内的总质数:5

  3. # 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());
        }
    }
    
  4. # 4 楼答案

    将生成的素数保存到列表中,最后生成输出。例如:

    System.out.println("The number of prime is: " + list.size());
    foreach(int prime : list)
        System.out.print(prime + ", ");