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);
}
}
}
}
我的输出显示为
欢迎使用Prime范围计算器!
输入下限: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
当我想要按顺序对素数的每一行进行编号时。
我该怎么做?
答案 0 :(得分:2)
在int counter = 0;
循环之前之前添加一个for
。然后更改
System.out.println(i);
到
System.out.printf("%d %d%n", ++counter, i);
此外,2
是质数。