package computing.primes;
import java.util.Scanner;
public class ComputingPrimes {
public static void main(String[] args) {
System.out.println("Welcome to the Prime Range Calculator!");
int sc1, sc2, sc3, flag = 0, i, j;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the lower limit : ");
sc1 = sc.nextInt();
System.out.print("Enter the upper limit : ");
sc2 = sc.nextInt();
System.out.println("The prime numbers between the limits are : ");
for(i = sc1; i <= sc2; i++)
{
for(j = 2; j < i; j++)
{
if(i % j == 0)
{
flag = 0;
break;
}
else
{
flag = 1;
}
}
if(flag == 1)
{
System.out.println(i);
}
}
}
}
我的输出读作
欢迎使用主范围计算器!
输入下限:1
输入上限:100
极限之间的素数是:
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
当我希望它按顺序对素数的每一行进行编号时。
我该如何去做呢?
最佳答案
在for
循环之前添加一个int counter = 0;
。然后更改
System.out.println(i);
至
System.out.printf("%d %d%n", ++counter, i);
此外,2
是质数。
关于java - 如何在素数范围程序的输出中打印行号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53074835/