打印1到1000内所有的素数,每行输出5个
程序源码:
public class NestCycle5 {
public static void main(String[] args) {
int count = 0;//记录每行打印的次数
for(int i = 1;i <1000;i++) {
boolean flag = true;//开关,记录当前是不是素数:true表示是素数
for(int j =2;j<i;j++) {
if(i%j==0) {
flag = false;
//当前i不是素数
break;
}
}
if(flag) {
System.out.print(i+" ");
count++;
if(count>=5) {
System.out.println();//换行
count = 0;
}
}
}
}
}
运行结果: