java求范围内的完数
编程找出m~n之间的所有完数,m和n为从键盘输入的两个整数。一个数如果恰好等于它的因子之和,这个数就称为 "完数 "。例如6=1+2+3。package perfectnum;
import java.util.Scanner;
public class PerfectNum {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int m = in.nextInt();
int n = in.nextInt();
System.out.print(m);
System.out.print(" to"+n);
System.out.println(" WanShu:");
for(int i = m;i <= n;i++) {
int sum = 0;//查找因数
for(int j = 1;j < m;j++) {
if(i % j == 0) {
sum = sum + j;//因数之和
}
if(sum == i) {//如果因数之和等于数本身则输出
System.out.print(i);
System.out.print(",");
}
}
}
}
}