检测只需检测到该数的平方根就可以了
package com.sun.xu;
public class Prime {public static void main(String[] args) {
boolean flag = true;//设置开关,当判断一次不能确定结果时就使用开关
int count = 0;
for (int i = 2; i <= 100; i++) {
for (int j = 2; j <=Math.sqrt(i); j++) {
if (i % j == 0) {
flag = false;
break;
}
}
if (flag) {
System.out.print(i + " ");
count++;
if (count % 10 == 0) {
System.out.println();
}
} else {
flag = true;
}
}
}
}