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

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

 1 public class ZhiShu {
 2     public static void main(String[] args) {
 3         //boolean f = true;  用来判断是否为质数,true是,否则不是  f变量不能写在for循环外面,要给他赋值true每回合。否则if(!f)这里永远都能跑到!
 4         int count = 0; //计数器,用于判断每几个数换行的。
 5         for(int i=101;i<=200;i++) {
 6             boolean f = true;
 7             for(int j=2;j<i;j++) {
 8                 if(i%j == 0) {
 9                     f = false;
10                     break;
11                 }
12             }
13             
14             if(f) {
15                 System.out.print(i+" ");
16                 count++;
17                 if(count%3 == 0) {        //这里要求每输出3个一换行
18                     System.out.println();
19                 }
20             }else {
21                 continue;
22             }
23             /* 
24             if(!f) {
25                 continue;
26             }
27             System.out.print(i+"	"); */
28             
29         }
30     }
31 }

运行结果:

原文地址:https://www.cnblogs.com/hangaozu/p/6219386.html