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 Range Calculator!
输入下限: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
当我希望它按顺序对质数的每一行进行编号时。
我将如何去做?
最佳答案
在int counter = 0;
循环之前添加for
。然后改变
System.out.println(i);
至
System.out.printf("%d %d%n", ++counter, i);
此外,
2
是质数。