怎么把所有素数的总和放在前面,然后是这些素数本身。

huangapple go评论95阅读模式
英文:

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

问题

我目前正在使用这段代码,我试图首先输出质数的总和,然后是质数列表。

以下是我的代码:

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("输入数字:");
        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);
    }
    
}

这是我得到的结果:

输入数字:10

2 3 5 7 N = 4

但是我所期望的结果是:

输入数字:10

N = 4
2 3 5 7

我不知道如何实现,我尝试了将S.O.P放在不同的位置,但是我无法弄清楚如何实现。

英文:

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(&quot;Input number: &quot;);
        r = sc.nextInt();
        
        System.out.println();
        
        for (int i = 2; i &lt; r; i++) {
            
            int f = 0;
            
            for (int j = 2; j &lt; i; j++) {
                
                if (i%j == 0)
                    f = 1;
            }
            
            if (f == 0) {
                
                System.out.print(i+ &quot; &quot;);
                s = s +1;
            }
            
        }
        System.out.println(&quot;N =&quot; +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 S.O.P in different places and I can't figure out how.

答案1

得分: 0

如果你想在显示 N = 4 之前显示 2 3 5 7,你可以将这些数字(即 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);
    }
}

一个示例运行:

Input number: 10

N = 4
2 3 5 7

注意: 检查素数性质,检查到 Math.sqrt(i) 就足够了,也就是说,你应该将 j < i 替换为 j <= Math.sqrt(i)

英文:

If you want to display N = 4 before 2 3 5 7, you can append the numbers (i.e. 2 3 5 7) to a StringBuilder and print the same after printing N = 4 e.g.

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(&quot;Input number: &quot;);
		r = sc.nextInt();

		System.out.println();

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

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

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

			if (f == 0) {
				numbers.append(i).append(&#39; &#39;);// Append to StringBuilder instead of printing
				s = s + 1;
			}
		}
		System.out.println(&quot;N =&quot; + 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 i.e. you should replace j &lt; i with j &lt;= Math.sqrt(i).

答案2

得分: 0

你可以将数字存储在一个列表中,然后在循环结束后打印它们。

不用担心如果你还没有学习过列表,因为不久之后你会需要用到它们。

以下是代码部分:

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

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

    System.out.print("输入数字:");
    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) {
            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) + " ");
    }
}
英文:

You can store the numbers in a list and then print them after the loop is over <br>
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&lt;Integer&gt; list = new LinkedList&lt;&gt;();

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

    System.out.println();

    for (int i = 2; i &lt; r; i++) {

        int f = 0;

        for (int j = 2; j &lt; i; j++) {

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

        if (f == 0) {

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

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

    for(int i = 0; i&lt;list.size(); i++ ) {
        System.out.print(list.get(i) + &quot; &quot;);
    }
}

huangapple
  • 本文由 发表于 2020年10月28日 02:44:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/64561012.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定