在双重for循环中 要记得将变量初始化很重要

计算完数的题

我错的忘记了

amount 需在第一重循环中初始化值为“0”

public class Test {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        int amount;
        System.out.println("所有完数是");
        for(int i = 1; i<=1000 ; i++){
            amount = 0;   //初始化 漏了这一步
            for(int j = 1; j < i ; j++){
                if(i%j==0){
                    amount = amount+j;
//                    System.out.println(amount);
                
            }
            
        }if(i == amount){
                System.out.println(""+i);
        }
    }

}
}

猜你喜欢

转载自www.cnblogs.com/zhou-zhe/p/13161440.html