【问题标题】:Trouble Understanding the Recursion Here麻烦理解这里的递归
【发布时间】:2023-03-10 00:55:01
【问题描述】:

有人可以帮我解释一下为什么这会导致无限递归循环吗?

可变长度达到值 1,但由于某种原因,即使循环条件为 while (length>1),仍会进入循环。

我已经尝试过打印值并一遍又一遍地运行它,也许我遗漏了一些更明显的东西,或者有人可以更简单地解释这一点。谢谢。

public static void main(String[] args) {
    xMethod(5);
}

public static void xMethod(int length) { 
    while (length > 1) {
        System.out.print((length - 1) + " ");
        xMethod(length - 1);
    }
}

其他信息。

当我调试这段代码时:

public static void main(String[] args) {
    xMethod(5);
}

public static void xMethod(int length) { 
    while (length > 1) {
        System.out.print((length - 1) + " ");
        xMethod(length - 1);
    }
    System.out.println("Coming out of while");
}

下面是输出:

4 3 2 1 Coming out of while
1 Coming out of while
1 Coming out of while
1 Coming out of while
1 Coming out of while
1 Coming out of while
1 Coming out of while
//repeated Infinite times

从while 循环出来后,为什么它会回到与length 相同的while 循环中作为2?

编辑:我感谢您的所有回复并理解,如果我想编写这样的代码,我可能会像大多数递归方法一样使用 if 语句,但这只是我可能不了解范围或调用方式的问题堆栈工作。如果我是正确的,那么无论在该块之外发生什么,while 循环块都会将长度值保持为 2?

【问题讨论】:

  • 你的预期输出是什么?
  • @TimBiegeleisen 当我调试有问题的代码时,我发现在 while(1>1) 条件之后它退出循环,然后再次返回到 while 循环 with while (2>1)这是怎么发生的?
  • 能否请您添加一段代码应该做什么的描述?
  • 循环是无限的,因为长度在循环内不会改变。如果您使用 if 语句而不是 while 语句,则可能不需要循环。递归是循环的替代品。
  • @bert 这似乎是最明智的答案。那里甚至不应该有一个循环,因为递归会隐式递减计数器。

标签: java recursion while-loop


【解决方案1】:

你在这里做两件事。在编写递归代码时,您总是需要考虑代码何时结束。您的代码没有结束案例。

public static void main(String[] args) {
             xMethod(5);
}

public static void xMethod(int length) { 

     System.out.println("Method Start "+ length);
        while (length > 1) {

            System.out.println("Inside while "+ length);

             xMethod(length - 1);
        }
        System.out.println("Method End "+ length);                 
    }
}

现在这段代码产生以下输出:

Method Start 5
Inside while 5
Method Start 4
Inside while 4
Method Start 3
Inside while 3
Method Start 2
Inside while 2
Method Start 1
Method End 1
Inside while 2
Method Start 1
Method End 1
Inside while 2
Method Start 1
Method End 1
Inside while 2
Method Start 1
Method End 1
.
.

如你所见,

Inside while 2
Method Start 1
Method End 1

一遍又一遍地重复。

那么这意味着,当长度为2时,会发生以下情况。

while (2 > 1) {
     System.out.println("Inside while "+ length);
     xMethod(1);
}

这个的输出是

Inside while 2

现在,xMethod(1) 甚至没有进入 while 循环,所以会打印出来。

Method Start 1
Method End 1

但是您现在应该明白while(2>1) 再次执行,因为长度没有改变,它仍然是2。

while (2 > 1){
    System.out.println("Inside while "+ length);
    xMethod(1);
}

继续,循环继续。

【讨论】:

  • @NicholasCorliss 很高兴,您发现它很有用。
【解决方案2】:

因为您没有更新当前方法中的长度值。发送到方法时,该值只是递减。

public static void main(String[] args) {
    xMethod(5);
}

public static void xMethod(int length) {
    while (length > 1) {
        System.out.print((length) + " ");
        xMethod(length);
        length--;
    }
}

【讨论】:

  • 从功能上讲,你的答案和OP中的代码没有区别。 OP 的代码很好,正在运行,但我认为对于 为什么 该代码正在运行存在一些混淆。
  • @TimBiegeleisen 不,OP 的问题中长度没有减少,因此 while 循环在无限循环中运行。
  • xMethod(length - 1) 递减计数,因此递归链中的下一个调用以比前一个调用小一的长度开始。这将继续,直到长度变为零。
  • @SandeshGupta 它与 OP 问题相同。
  • 否 @TimBiegeleisen,当前代码的输出是 5 4 3 2 2 2 2 2 2 2 2 2 2 2 2..........。但是这段代码产生的输出为4 3 2 1 1 2 1 1 3 2 1 1 2 1 1 。
【解决方案3】:

可变长度在任何循环中都不会达到值 1,您混合了两种设计,我认为您需要其中的一种,递归方法或循环。

第一个设计:

public static void main(String[] args) {
    xMethod(5);
}
public static void xMethod(int length) { 
    System.out.print((length - 1) + " ");
    if(length > 1)
        xMethod(length - 1);
    }
}

另一种方式:

public static void main(String[] args) {
    xMethod(5);
}
public static void xMethod(int length) {
    while (length > 1) {
        System.out.print((length--) + " ");
    }
}

您可以选择其中之一,这取决于您的设计。 如果这不是你的答案,请写下你的期望输出。

【讨论】:

    【解决方案4】:

    因为当length 到达2 xMethod(2) 被调用,因此xMethod(1) 跟随,当xMethod(1) 结束时,因为length 仍然是2 它再次调用xMethod(2) 并且this 调用@ 987654329@它重复..

    要修复它,请在xMethod(length - 1); 之后使用return

    public static void main(String[] args){
            xMethod(5);
        }
    
        public static void xMethod(int length) { 
    
            while (length > 1) {
    
                System.out.print((length - 1) + " ");
    
                 xMethod(length - 1);
                 return;
            }
            System.out.println("Coming out of while");
        }
    

    【讨论】:

      猜你喜欢
      • 2021-02-12
      • 1970-01-01
      • 2019-05-08
      • 2013-10-30
      • 1970-01-01
      • 1970-01-01
      • 2015-10-22
      • 2015-07-04
      • 2017-06-21
      相关资源
      最近更新 更多