简体   繁体   English

Java Recursive Method它是如何工作的?

[英]Java Recursive Method how does it work?

I'm relatively new to Java programming and I've just started learning recursion, but I can't seem to figure out how this method works in my head. 我对Java编程比较陌生,我刚刚开始学习递归,但我似乎无法弄清楚这种方法是如何工作的。

   private static int mystery(int w) {
    {
        if (w < 0) return 0;
        int x = mystery (w-2);
        return w - x;
    }
}

Whenever a variable like 100 is put in, it outputs 50. When 200 is input, it outputs 100. When 2 is input, it outputs 2. When 25 is input, 13 is output. 每当输入100这样的变量时,输出50.当输入200时,输出100.当输入2时,输出2.当输入25时,输出13。 I'm not sure how this method works, and I'm trying to wrap my head around it. 我不确定这种方法是如何工作的,我正试图绕过它。

The way I currently view it, if you put in 100, it'll bypass the first return statement since it is greater than 0. when it gets to the second line, it'll do 100-2, which brings in 98, then goes to the third line and does 100 - 98 = 2. Which is then returned to the original call. 我目前查看它的方式,如果你输入100,它将绕过第一个return语句,因为它大于0.当它到达第二行时,它会做100-2,这会带来98,然后转到第三行并执行100 - 98 = 2.然后返回到原始呼叫。

I know I'm messing up on the second line of the method where the mystery (w-2) is. 我知道我搞砸了神秘(w-2)方法的第二行。 I assume it would bring back the result of w-2 to the beginning of the method again, and it would continue to do the method over and over again until w is smaller than 0, which should output 0 again regardless of the answer. 我假设它会再次将w-2的结果带回到方法的开头,并且它会一遍又一遍地继续执行该方法,直到w小于0,无论答案如何,都应该再次输出0。 But that's not what happens, and I don't know why. 但事实并非如此,我不知道为什么。

Can anyone explain what is going on here? 谁能解释一下这里发生了什么?

What you are missing is that on the second line it doesn't just do w - 2, but calls itself with w - 2. It doesn't go further until the call returns. 你缺少的是,在第二行它不仅仅做w - 2,而是用w - 2 调用它 。它在呼叫返回之前不会更进一步。 And the second call calls itself if w isn't < 0 and so on until you reach value lower than 0 and then return. 如果w不是<0,则第二个调用会自行调用,直到达到低于0的值然后返回为止。 The execution will go like this, if you visualize it: 如果您想象它,执行将如下所示:

mystery(10)
    > skip first line
    > x = mystery(8)
        > skip first line
        > x = mystery(6)
            > skip first line
            > x = mystery(4)
                > skip first line
                > x = mystery(2)
                    > skip first line
                    > x = mystery(0)
                        > skip first line
                        > x = mystery(-2)
                            > return 0
                        > return 0 - 0 (0)
                    > return 2 - 0 (2)
                > return 4 - 2 (2)
            > return 6 - 2 (4)
        > return 8 - 4 (4)
    > return 10 - 4 (6)

With example of w = 10. I hope you understand it better now. 以w = 10为例。我希望你现在能更好地理解它。

   private static int mystery(int w) {
    {
        if (w < 0) return 0;
        int x = mystery (w-2);
        return w - x;
    }
}

Let's imagine that we call mystery(3) . 让我们想象一下,我们称之为mystery(3) What happens? 怎么了? w<0) is false, so we don't return 0. In the next line, we call some function called mystery using the value 3-2=1 as its argument. w<0)是假的,所以我们不返回0.在下一行中,我们使用值3-2 = 1作为参数调用一些名为mystery的函数。 Despite the fact that this function we've called happens to be the same one we've just called, it's still an ordinary function call, and it returns a value. 尽管我们调用的函数恰好与我们刚刚调用的函数相同,但它仍然是一个普通的函数调用,它返回一个值。 It does this by calling the function called mystery , this time using the value -1 as the argument. 它通过调用名为mystery的函数来完成此操作,这次使用值-1作为参数。 And this time w<0 is true, so we just return 0. Now we're back in the second call to mystery , and we've set x = 0. So that call returns w - 0 = 1. That puts us back in the first call, and now x = 1, so we return wx = 3-1 = 2. 而这次w<0是真的,所以我们只返回0.现在我们回到第二次调用mystery ,我们设置x = 0.所以这个调用返回w - 0 = 1.这让我们回来了在第一次调用中,现在x = 1,所以我们返回wx = 3-1 = 2。

You might want to take a few minutes and work through this using w=4 and see what you get - this will help you understand how the recursive calls work. 您可能需要花几分钟时间使用w = 4来完成此操作并查看所获得的内容 - 这将有助于您了解递归调用的工作原理。 After you've done this, I suggest you add a print statement or two in the function to tell you where you are and what's happening, and that'll also help - but do it on paper first. 在你完成这个之后,我建议你在函数中添加一两个打印语句,告诉你你在哪里以及发生了什么,这也会有所帮助 - 但是先在纸上做。

The two given answers are excellent. 这两个给出的答案非常好。 Both focus on the way how to get a grasp of what recursion is. 两者都侧重于如何掌握递归的方式。 The problem with recursion is, that it is so unnatural to one who do not know what recursion is, or do not know someone who does. 递归的问题在于,对于不知道递归是什么,或者不知道有人做过的人来说,它是如此不自然。 It's like a snake eating itself again and again. 这就像一条蛇一次又一次地吃着自己。

The best way to understand recursion is to write down the calls to a recursive method, by noying the current state when it's called, and after the call write the result back. 理解递归的最好方法是记下对递归方法的调用,通过在调用时忽略当前状态,并在调用之后写回结果。 You stack up the calls and that's also the way to not used recursion at all. 你堆叠调用,这也是不使用递归的方法。

So do not try too hard to understand recursion at first but first focus on the program flow. 因此,首先不要太努力理解递归,但首先要关注程序流程。 If you have seen enough recursions, it will come to you. 如果你已经看到了足够的递归,它会来找你。

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

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