public class DisplayPrime {
public static void main(String[] args) {
int count =0;
final int NUMBER_OF_PRIMES_PER_LINE = 8;
boolean isPrime = true;
for(int i = 2; i <= 1200; i++) {
for(int divisor = 2; divisor <= i / 2; divisor++) {
if(i % divisor == 0) {
isPrime = false;
break;
}
}
if(isPrime) {
count++;
if(count % NUMBER_OF_PRIMES_PER_LINE ==0) {
System.out.println(i);
}
else
System.out.print(i + " ");
}
}
}
}
输出只有2和3,没有其他质数
boolean isPrime = true;
放到两个for循环之间,也就是第六行那里
因为i=4之后isPrime变成False再也没有变成True
望采纳,谢谢