Java第三章习题3-6(完数)

来源:互联网 发布:联通网络测速手机版 编辑:程序博客网 时间:2024/04/29 18:20

Number.java

/* * To change this template, choose Tools | Templates * and open the template in the editor. *//** * * @author Administrator */public class Number {    public void sum(){        int i=0,j=0,r=0;        int num=1000;        for(i=1;i<num;i++){            r=0;            for(j=1;j<i;j++){  //找到所有因子并把他们相加                if(i%j==0){                    r=r+j;                }            }            if(r==i){   //判断是否是完数                System.out.println(r);            }        }                }    }


Test.java

/* * To change this template, choose Tools | Templates * and open the template in the editor. *//** * * @author Administrator */public class Test {    public static void main(String[] args){        Number n=new Number();        n.sum();    }    }


0 0
原创粉丝点击