【JAVA习题二】判断101-200之间有多少个素数,并输出所有素数。

package XXXX;
public class Sushu素数 {
    public static int count = 0;
    public static void main(String[] args) {
        for (int i = 101; i < 200; i++) {
            boolean b = true;//默认此数就是素数
            for (int j = 2; j <= Math.sqrt(i);j++){
                if(i%j ==0){
                    b = false;
                    break;
                }
            }
            if(b){
                count++;
                System.out.print(i + " ");
            }
        }
        System.out.println("
素数的个数:"+count);
    }
}

关于public static int count = 0;:因为count是静态变量,静态变量存储在静态存储区,只会被初始化一次,在执行程序时,会对它的值进行改变,当再次要用到它时,它的数据是在上一次操作之后的基础上进行改变的。

原文地址:https://www.cnblogs.com/chenxi1944362410/p/12955742.html