问题
I am currently working on java code that will allow me to print out all perfect numbers below 10,000. My issue is that I can not figure out why my code is not printing 6, but is printing all the other perfect numbers. My code is below, please send help if you can see what I have over looked. Thank you,
int min = 1;
int max = 10000;
for (min = 1; min <= max; min++) {
int sum = 0;
int e = 1;
for (e = 1; e < min; e++) {
int a = min % e;
if (a == 0) {
sum += e;
}
}
if (sum == min){
System.out.println(sum);
}
}
回答1:
Your solution should be fine, but if still has trouble, try to clear then rebuild.
my code listed below gets the correct answers:
public static void main(String[] args){
int min = 1;
int max = 10000;
for (min = 1; min <= max; min++) {
int sum = 0;
for (int e = 1; e < min; e++) {
if ((min % e) == 0) {
sum += e;
}
}
if (sum == min){
System.out.println(sum);
}
}
}
来源:https://stackoverflow.com/questions/32633892/java-program-to-find-perfect-numbers-below-10-000