简体   繁体   English

怎么先求素数的总和,再放下面的素数

[英]How do i put the total sum of prime numbers first, then the prime numbers below it

I'm currently using this code, and I'm trying to output the total sum of how many prime numbers first and then the prime numbers list我目前正在使用此代码,我试图先输出多少素数的总和,然后输出素数列表

Here is my code:这是我的代码:

import java.util.*;

public class PrimeTime {

    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        
        int r, s = 0;
        
        System.out.print("Input number: ");
        r = sc.nextInt();
        
        System.out.println();
        
        for (int i = 2; i < r; i++) {
            
            int f = 0;
            
            for (int j = 2; j < i; j++) {
                
                if (i%j == 0)
                    f = 1;
            }
            
            if (f == 0) {
                
                System.out.print(i+ " ");
                s = s +1;
            }
            
        }
        System.out.println("N =" +s);
    }
    
}

This is the result I get:这是我得到的结果:

Input number: 10

2 3 5 7 N = 4

But what I'm looking for is this result:但我正在寻找的是这个结果:

Input number: 10

N = 4
2 3 5 7

I don't know how, I tried putting the SOP in different places and I can't figure out how.我不知道怎么做,我试着把 SOP 放在不同的地方,但我不知道怎么做。

If you want to display N = 4 before 2 3 5 7 , you can append the numbers (ie 2 3 5 7 ) to a StringBuilder and print the same after printing N = 4 eg如果要在2 3 5 7之前显示N = 4 ,可以将数字(即2 3 5 7 )附加到StringBuilder并在打印N = 4后打印相同的数字,例如

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int r, s = 0;
        StringBuilder numbers = new StringBuilder();

        System.out.print("Input number: ");
        r = sc.nextInt();

        System.out.println();

        for (int i = 2; i < r; i++) {
            int f = 0;

            for (int j = 2; j <= Math.sqrt(i); j++) {

                if (i % j == 0)
                    f = 1;
            }

            if (f == 0) {
                numbers.append(i).append(' ');// Append to StringBuilder instead of printing
                s = s + 1;
            }
        }
        System.out.println("N =" + s);
        System.out.println(numbers);
    }
}

A sample run:示例运行:

Input number: 10

N =4
2 3 5 7 

Note: for checking the primality , checking up to Math.sqrt(i) is sufficient ie you should replace j < i with j <= Math.sqrt(i) .注意:为了检查素数,检查到Math.sqrt(i)就足够了,即你应该用j <= Math.sqrt(i)替换j < i

You can store the numbers in a list and then print them after the loop is over您可以将数字存储在列表中,然后在循环结束后打印它们
Don't worry if you have not studied lists yet cause you will have to soon anyways.如果您还没有研究过列表,请不要担心,因为无论如何您很快就会不得不学习。

here's the code:这是代码:

 public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    int r, s = 0;
    LinkedList<Integer> list = new LinkedList<>();

    System.out.print("Input number: ");
    r = sc.nextInt();

    System.out.println();

    for (int i = 2; i < r; i++) {

        int f = 0;

        for (int j = 2; j < i; j++) {

            if (i%j == 0)
                f = 1;
        }

        if (f == 0) {

            //System.out.print(i+ " ");
            list.add(i);
            s = s +1;
        }

    }
    System.out.println("N = " +s);

    for(int i = 0; i<list.size(); i++ ) {
        System.out.print(list.get(i) + " ");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM