public static void main(String[] args) {
int count=0;
a: for (int i = 2; i <= 100; i++) {//遍历2到100的所有数,在这里由于1和2不会进入内循环,所以需要自己判断,1不是素数,所以不从1开始。
for (int j = 2; j < i; j++) {//遍历2到i-1的所有数
if (i % j == 0)//如果i能被j整除,说明不是素数。
continue a;//不是素数就跳出本次循环,跳到外层循环。
}
count++;//非素数已经跳出了本次循环,能执行到这来的都是素数,计数器加1
if(i<10)
System.out.print(i+" ");//强迫对齐,如果i小于10则打印两个空格
else
System.out.print(i+" ");//大于10则打印一个空格
if(count%5==0)
System.out.println();//计数器是5的倍数则打印一个换行
}
System.out.println("count="+count);//在循环外面打印count
} |