简体   繁体   English

Java递归误解

[英]Java Recursivity misunderstanding

I'm not understanding this code, why it counts up. 我不明白这段代码,为什么要计数。 If I change the order in the print statement with recursivity call It makes sense to me, but as it is why it is counting up. 如果我通过递归调用更改print语句中的顺序,这对我来说很有意义,但这就是为什么它递增计数的原因。 In by book it says that "System.out.println happens just before each recursive call returns. As a result, it counts up instead of down." 在书中,它说:“ System.out.println恰好在每个递归调用返回之前发生。因此,它是递增计数而不是递减计数。” And I am not understanding It. 我不明白。 Appreciate your help. 感谢您的帮助。

    public static void countdown(int n)
    {
         if (n == 0) 
         {        
              System.out.println("Blastoff!");    
         } 
         else 
         {
               countdown(n - 1); 
               System.out.println(n);        

         } 
   }

So, if n != 0, your program running code in "else" block, where is another call to method countdown(n-1). 因此,如果n!= 0,则程序在“ else”块中运行代码,其中是对方法countdown(n-1)的另一次调用。 For example, if you put n = 3, this code will be running as long as n > 0. So, basiclly running method run herself, looks like this: 例如,如果您将n = 3,则只要n> 0,此代码就会一直运行。因此,基本上运行的方法自己运行,如下所示:

countdown(3) call method countdown(2), and then countdown(2) call countdown(1). countdown(3)调用方法countdown(2),然后countdown(2)调用countdown(1)。 It will happen as long as n will be higher than 0. If n == 0, it will print Your message. 只要n大于0,它就会发生。如果n == 0,它将打印您的消息。

you should change the condition 'n == 0' to 'n <=0'. 您应将条件“ n == 0”更改为“ n <= 0”。 because if you pass negative value then it wont stop and you might see negative number. 因为如果传递负值,它将不会停止,并且您可能会看到负数。

lets says if you passed n = -3. 让我们说如果您通过了n = -3。 then it would keep printing -3, -4...etc. 那么它将继续打印-3,-4 ...等。

countdown(n - 1);
System.out.println(n);

It indeed counts up. 它的确计数。

Let's take a look at what actually happens: Each countdown call first calls itself, even before anything is written to System.out . 让我们看一下实际发生的情况:每个countdown调用都会首先调用自己,甚至没有将任何内容写入System.out

In the following example, let's say I call countdown with 2 as argument. 在以下示例中,假设我使用2作为参数调用countdown

  1. countdown(2) is called countdown(2)被称为
  2. within this method call, n == 2 , so else block is executed 在此方法调用中, n == 2 ,因此执行else
  3. countdown(1) is called countdown(1)被称为
  4. within this method call, n == 1 , so else block is executed 在此方法调用中, n == 1 ,因此执行else
  5. countdown(0) is called countdown(0)被称为
  6. within this method call, n == 0 thus the if-condition is true, so "Blastoff!" 在此方法调用中, n == 0因此if条件为true,因此“ Blastoff!” is printed 打印
  7. this method exits, returning to the method denoted by step 3. 该方法退出,返回到步骤3表示的方法。
  8. n is printed, which has the value 1. 将打印n ,其值为1。
  9. the method exits, returning to the method denoted by step 1. 方法退出,返回到步骤1表示的方法。
  10. n is printed, which has the value 2. 将打印n ,其值为2。
  11. the method exits 方法退出

Note that each method call has its own local variables, like n . 请注意,每个方法调用都有其自己的局部变量,例如n So the output is: 因此输出为:

Blastoff!
1
2

as expected. 如预期的那样。 You see that, just according to what the book says, the method calls itself prior to printing something to sysout. 您会发现,按照书中所说,该方法在将某些内容打印到sysout之前会先调用自身。

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

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