简体   繁体   English

在这里无法理解递归

[英]Trouble Understanding the Recursion Here

Can someone please help to explain to me why this ends up in an infinite recursion loop? 有人可以帮我解释一下为什么这会导致无限递归循环吗?

The variable length reaches the value 1 but for some reason the loop is still entered even though the loops condition is while (length>1). 可变长度达到值1,但是由于某种原因,即使循环条件为while(length> 1),仍会进入循环。

I've tried printing values and running it over and over again, maybe I'm missing something more obvious or someone can explain this more simply. 我尝试过打印值并一遍又一遍地运行它,也许我错过了一些更明显的东西,或者有人可以更简单地解释这一点。 Thanks. 谢谢。

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);
    }
}

Additional info. 附加信息。

When I dubugged this code : 当我调试此代码时:

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");
}

Below is the output : 以下是输出:

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

After coming out of while loop why it is going back in the same while loop with length as 2? while循环出来后while为什么要返回相同的while循环, length为2?

Edit: I appreciate all of your responses and understand that if I wanted to code something like this I would probably us an if statement as most recursive methods do but this is simply a question of me perhaps not understanding how the scope or call stack works. 编辑:我感谢您的所有答复,并且理解,如果我想像这样编写代码,我可能会像大多数递归方法一样使用if语句,但这只是我的一个问题,也许是我不了解范围或调用堆栈的工作原理。 If I'm correct the while loop block holds on to the value of length as 2 no matter what happens outside of that block? 如果我是正确的,那么无论该循环在块外发生了什么,while循环块都将length的值保持为2。

Because you are not updating the value of length in the current method. 因为您没有在当前方法中更新length的值。 The value is just decremented when sending to the method. 发送到方法时,该值仅递减。

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

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

The variable length never reaches the value 1 in any loop, you mix two design and i think you need on of them, recursive method or loop. 可变长度在任何循环中都永远不会达到值1,您混合使用两种设计,我认为您需要使用它们,即递归方法或循环。

first design: 第一个设计:

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);
    }
}

another way: 其他方式:

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

you can select one of these, it depend on your design. 您可以选择其中之一,这取决于您的设计。 if it's not your answer, please write your expect output. 如果不是您的答案,请写出您的期望输出。

You are doing 2 things here. 您正在这里做两件事。 When you are writing a recursive code, you always need to think when the when code will end. 在编写递归代码时,您始终需要考虑何时代码何时结束。 Your code does not have a end case. 您的代码没有结尾。

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);                 
    }
}

Now this code produces the following output: 现在,此代码将产生以下输出:

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
.
.

As you can clearly see, 如您所见,

Inside while 2
Method Start 1
Method End 1

is repeated again and again. 一次又一次地重复。

So what this means is, when the length is 2, the following will happen. 所以这意味着,当长度为2时,将发生以下情况。

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

The output for this is 输出是

Inside while 2

Now, xMethod(1) doesn't even enter the while loop, so this will be printed. 现在, xMethod(1)甚至都没有进入while循环,因此它将被打印出来。

Method Start 1
Method End 1

But you should now understand that while(2>1) is again execute because the length has not changed and it is still 2 . 但是,您现在应该了解,再次执行了while(2>1) ,因为长度没有改变,并且仍然为2

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

goes on and the loop continues. 继续,循环继续。

because when length reaches 2 xMethod(2) is called and therefore xMethod(1) follows, when xMethod(1) ends, since length is still 2 it again calls xMethod(2) and the this calls xMethod(1) it repeates.. 因为当length达到2 xMethod(2)并因此xMethod(1) ,所以当xMethod(1)结束时,由于length仍为2它将再次调用xMethod(2)并再次调用xMethod(1)

to fix it, use return after xMethod(length - 1); 要修复它,请在xMethod(length - 1);之后使用return 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);
             return;
        }
        System.out.println("Coming out of while");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM