题目:判断101-200之间有多少个质数,并输出所有质数。

程序分析:首先明白什么是质数,只能被1和本身整除的数,用循环遍历101-200之间的数,然后用101~200间的书整出2到该数前面一个数,比如是113,我们113整除2~112之间的数,只要这里的数整出都不等于0,则可以判断这个数是质数;

public class ZhiShu {
public static void main(String[] args) {
//boolean f = true; 用来判断是否为质数,true是,否则不是 f变量不能写在for循环外面,要给他赋值true每回合。否则if(!f)这里永远都能跑到!
int count = 0; //计数器,用于判断每几个数换行的。
for(int i=101;i<=200;i++) {
boolean f = true;
for(int j=2;j<i;j++) {
if(i%j == 0) {
f = false;
break;
}
}

if(f) {
System.out.print(i+" ");
count++;
if(count%3 == 0) { //这里要求每输出3个一换行
System.out.println();
}
}else {
continue;
}
/*
if(!f) {
continue;
}
System.out.print(i+" "); */

}
}
}

运行结果:
题目:判断101-200之间有多少个质数,并输出所有质数。 - 飞雪安能住酒中 - 飞雪安能住酒中的博客
原文地址:https://www.cnblogs.com/hangaozu/p/7544474.html