英文:
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("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 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("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 i.e. you should replace j < i
with j <= 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<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) + " ");
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论