<分区>
语句是:写一个程序,读取一个整数N并打印前N个素数。
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
int x = 2;
for(int i = 0; i <= N; i++)
{
int count = 0;
for(int j = 1; j <= x; j++)
if(x%j == 0)
count++;
if(count == 2)
System.out.print(x + " ");
x++;
}
}
当我运行这段代码时,它没有给我确切的 N 个数字。例如,对于 N=1 & 2,它打印前 2 个质数,对于 N = 3 & 4,它打印前 3 个质数,对于 N = 5 & 6,它打印前 4 个质数,依此类推。这段代码有什么问题?