1000以内的质数的方法

实体类 封装一个方法

public boolean IsPrimem(){
        if(value<=1){
            System.out.println(value+"小于2不能进行运算!");
            return false;
        }else{
            //重要的IT思想,标志位
            boolean flag=true;
            
            for(int i=2;i<value;i++){
                //System.out.println(value+"除以"+i+"的余数是:"+(value%i));
                if(value%i==0){
                    System.out.println("可以整除!");
                    flag=false;
                    break;//不用再循环
            }
          }
            return flag;
    }
}
    
    //属性,用于记录状态
    private int value;

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

jsp的调用

Calc calc=new Calc();
for(int j=1;j<=1000;j++){
    calc.setValue(j);
    if(calc.IsPrimem()){
    out.println(j+"|");
    }

猜你喜欢

转载自www.cnblogs.com/liangyaofeng/p/9247184.html