【问题标题】:How to prevent a recursive method from changing a value of a variable?如何防止递归方法更改变量的值?
【发布时间】:2022-06-28 21:33:55
【问题描述】:

我正在学习 Java,但遇到了递归问题。我需要使用递归方法来检查一个数字是否是 Armstrong 数字。代码如下:

public class ArmstrongChecker {

    public boolean isArmstrong(int number) {
        // check if the number is a negative number
        if (number < 0) {
            return false;
        }
        ArmstrongChecker armstrongChecker = new ArmstrongChecker();
        // find the length of the number
        int length = armstrongChecker.lengthChecker(number);
        // create a variable to store the sum of the digits of the number
        int sum = 0;
        // find the individual digits and raise to the power of the numbers of digits
        if (number != 0) {
            int digit = Math.floorMod(number, 10);
            int powerRaised = (int) Math.pow(digit, length);
            sum = sum + powerRaised;
            isArmstrong(number / 10);
        }
        return sum == number;
    }

    // method to check the length of the number
    public int lengthChecker(int number) {
        int length = String.valueOf(number).length();
        return length;
    }
}

如何防止 isArmstrong 方法中的 int length 改变其值

【问题讨论】:

  • 怎么改?每个级别的递归都有自己的length 值。
  • 如果你不想改变这个值,不要在方法内部调用它。另外,我和 akarnokd 在一起。
  • 您忽略了递归调用 isArmstrong() 的结果。并且没有必要生成 ArmstrongChecker 的新实例。

标签: java oop recursion


【解决方案1】:

虽然您没有在发布的代码中更改它的值,但您可以将该变量标记为常量。这样,如果您尝试分配新值,编译器可能会出错。

final int length = armstrongChecker.lengthChecker(number);

【讨论】:

    【解决方案2】:

    仅以number 作为参数很难做到这一点,所以我稍微编辑了该方法:

    class Main {
      public static void main(String[] args) {
        System.out.println(isArmstrong(555,555,0));
        System.out.println(isArmstrong(153,153,0));
      }
      public static boolean isArmstrong(int number, int copy, int curr){
        if(copy == 0 && curr == number){
          return true;
        }
        else if(copy == 0){
          return false;
        }
        curr += (int)(Math.pow(copy % 10, 3));
        copy -= copy % 10;
        copy /= 10;
        return isArmstrong(number, copy, curr);
      }
    }
    

    我们改用 3 个参数:numbercopycurrcopy 最初将存储与 number 相同的值,但我们会在每次递归访问时更改该值每个数字。 curr 将存储数字立方和的当前值。

    虽然copy 的值不是0,但我们将更新curr 的值,方法是将curr 的最后一位数字相加,然后通过减去最后一位数除以 10。

    我们重复这个过程直到copy0。如果curr 匹配number 的值,我们返回true,否则返回false

    你也可以创建一个辅助方法,这样当你调用函数时,你仍然只输入一个参数:

    class Main {
      public static void main(String[] args) {
        System.out.println(isArmstrong(555));
        System.out.println(isArmstrong(153));
      }
      public static boolean isArmstrong(int number){
        return isArmstrong(number, number, 0);
      }
      public static boolean isArmstrong(int number, int copy, int curr){
        if(copy == 0 && curr == number){
          return true;
        }
        else if(copy == 0){
          return false;
        }
        curr += (int)(Math.pow(copy % 10, 3));
        copy -= copy % 10;
        copy /= 10;
        return isArmstrong(number, copy, curr);
      }
    }
    

    两者的输出:

    false
    true
    

    我希望这会有所帮助!如果您需要任何进一步的帮助/说明,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-15
      • 2021-12-18
      • 1970-01-01
      • 2017-09-10
      • 1970-01-01
      • 2018-09-20
      • 1970-01-01
      • 2012-04-17
      相关资源
      最近更新 更多