必须创建列出所有完美数字(因子之和 = 数字)1 - 1000 的程序。
- 这是一个java类,只需要使用“for”循环
我检查了我的代码 100 次,但没有输出,我在某处遗漏了一个逻辑错误,有人可以帮我吗?
public static void main(String[] args)
{
// variables
int total = 0;
final int LIMIT = 1000;
// for loop to test all numbers 1-1000
for(int i = 1; i <= LIMIT; i++)
{
// if statement
if((i != 1) && (total == i - 1))
{
// prints perfect number
System.out.println((i - 1) + " is a perfect number");
// resets total value
total = 0;
}
// gets and add factors as total
for(int divider = 1; divider < i; divider++)
{
if((i % divider) == 0)
{
total += divider;
}
}
}
}