2

所以我的任务是找到一个数字范围内的所有“幻数”(由用户输入)。幻数是一个数,其因数(除了它自己)总和等于该数。所以 6 将是一个神奇的数字,因为它本身的因素是 1,2 和 3,总和为 6。我已经盯着这段代码一段时间了,我一生都无法弄清楚为什么它不会打印出来神奇的数字。任何帮助,将不胜感激。

public class MagicNumber {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.print("What is the top of the range?");
    int range = IO.readInt();
    if (range <= 0 ) {
        IO.reportBadInput();
    }
    int sumOfFactors = 0;
    for (int i = 1 ; i <= range ; i++) {
        for (int m = 1 ; m < i; m++) {
            if (i % m == 0) {
                sumOfFactors = sumOfFactors + m;
            }
            if (sumOfFactors == i) {
                System.out.println(i);
                    }
            }
        }
    }
}
4

5 回答 5

2

您正在测试是否sumOfFactors == i仍在对因子求和。您需要将其移到m循环之外。然后你需要在每次循环sumOfFactors开始循环之前设置为0 ,而不仅仅是在循环开始时设置一次。mi

于 2012-10-21T03:02:57.693 回答
0

试试这个:

 public class MagicNumber {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.print("What is the top of the range?");
        int range = IO.readInt();
        if (range <= 0 ) {
            IO.reportBadInput();
        }

        for (int i = 1 ; i <= range ; i++) {
   int sumOfFactors = 0;
            for (int m = 1 ; m < i; m++) {
                if (i % m == 0) {
                    sumOfFactors = sumOfFactors + m;
                }
    }
                if (sumOfFactors == i) {
                    System.out.println(i);
                        }

            }
        }
    }
于 2012-10-21T03:00:17.907 回答
0

我认为您需要int sumOfFactors = 0;在第一个循环内for初始化并将第二个循环if移出第二个for循环,因为您需要将总因子的总和与当前数字进行比较,如下所示:

for (int i = 1 ; i <= range ; i++) {
    int sumOfFactors = 0;  //<--Moved inside
    for (int m = 1 ; m < i; m++) {
        if (i % m == 0) {
            sumOfFactors = sumOfFactors + m;
        }
     }
     if (sumOfFactors == i) {  // <-- moveed out of second loop
       System.out.println(i);
     }
}
于 2012-10-21T03:09:16.260 回答
0

只是参考这个魔术数字程序 http://getprogramcode.com/2013/11/java-program-to-check-for-a-magic-number/ 并想到写这个。

public class HelloWorld{

     public static void main(String []args){
        static int number=5432;
        while(number>9){
            int tot = calculateSum(number);
            System.out.println(tot);
        }
     }

     private static int calculateSum(int num){
         int sum = 0;
         while (num > 0) {
            int a = num % 10;
            sum = sum+ a;
            num = num / 10;
        }
         number = sum;
         return sum;
     }
}
于 2014-11-24T18:34:41.190 回答
0
public class Magic {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int b =778393;
        int e=0,c;

        while (b>=10) {
            while (b>0) {
                c=b%10;
                b=b/10;
                e=e+c;
            }
            b=e;
            e=0;
            System.out.println(b);
        }
        if (b==1) {
            System.out.println("It is a magic no."+b);
        } else {
            System.out.println("Not"+b);
        }
    }
}
于 2015-10-24T13:19:55.713 回答