【问题标题】:"for" loop and perfect numbers [closed]“for”循环和完美数字[关闭]
【发布时间】:2014-06-09 20:56:54
【问题描述】:

必须创建列出所有完美数字(因子之和 = 数字)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;
      }
    }
  }
}

【问题讨论】:

  • 请分享您的代码。
  • 是的,请这样做。我们无法为您写下您的答案。但我们可以帮助您摆脱困境。

标签: java loops for-loop perfect-numbers


【解决方案1】:

你的大问题是你只有在找到一个完美的数字时才会重置total。如果您没有找到一个完美的数字,您将继续将下一个数字的除数添加到旧总数中。您需要为每个i 重新开始。

按以下方式重新安排程序应该会有所帮助:

public static void main(String[] args) {
    final int LIMIT = 1000;
    for (int i = 0; i <= LIMIT; i++) {
        // Declare total here, inside the loop, so values from previous
        // iterations are discarded.
        int total = 0;
        for (/* your code here */) {
            // add up divisors
            // your code here
        }
        // compare to i, rather than always computing the total for the
        // previous number and comparing to that.
        if (/* your code here */) {
            // print output
            // your code here
        }
    }
}

【讨论】:

    【解决方案2】:

    您应该将 total = 0; 移到 if 语句之外。您的总数不断增加,永远不会被重置。

    【讨论】:

    • 不仅如此,看看他在哪里加了total,在哪里查。
    • 他在接下来的迭代中检查它。我不知道他为什么要这样做,但它应该仍然按预期运行,可能除了循环提前结束一次迭代。
    • 啊,是的,我明白你的意思了。虽然我想检查给定的迭代会让它更容易理解。
    • 我完全同意 :) 我有点懒惰,然后@user2357112 决定发布一个不错的答案。我想他可以将 OP 的代码粘贴到这些 cmets 中:\
    • 当然。我假设他没有这样做,以便 OP 填写空白,而不仅仅是复制粘贴他放下的内容。
    【解决方案3】:

    我建议将您的算法拆分为不同的部分,这样您就可以一次专注于较小的任务。

    1. 找到数字i的因子。一种获取数字并返回其因子数组的方法。
    2. 将因子相加。一个简单的方法,它接受一个数组并返回它的总和。
    3. 主循环:只需检查 sum(factors(i)) == i

    您可以从更简单的方法开始(提示:2 和 3),然后可能会寻找一些并非完全低效的方法来实现 1

    【讨论】:

    • 很有可能他刚刚学习编程,刚刚开始学习循环,还没有开始创建方法:)
    【解决方案4】:

    (total == i - 1) 当你从 total=0 和 i>1 开始时永远不会匹配。

    【讨论】:

      【解决方案5】:

      你很亲密。只需要重新评估一下你的逻辑。

      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++) {
      
                  // gets and add factors first
                  for (int divider = 1; divider < i; divider++) {
                      if ((i % divider) == 0) {
                          total += divider;
                      }
      
                  }
      
                  // then check if sum == number
                  // also just print i instead of - 1
                  if ((i != 1) && (total == i)) {
                      // prints perfect number
                      System.out.println((i) + " is a perfect number");
      
                  }
      
                  // alway reset when we are done with a number
                  total = 0;
      
              }
      
          }
      

      【讨论】:

        【解决方案6】:
         for(int i=1;i<1000;i++)
            {
                int k=0;
        
                for(int j=1;j<i;j++)
                {
                    if(i % j==0)
                    {   
                        k+=j;
                    }
        
                }
                if(k==i)
                {
                    System.out.println(k);
                }
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-01-29
          • 1970-01-01
          • 2013-07-19
          • 1970-01-01
          • 2023-04-09
          • 1970-01-01
          • 2021-08-13
          相关资源
          最近更新 更多