简体   繁体   English

递归函数输出不清楚

[英]Recursive function output not clear

 public void numbers(int n)
 {
     if(n>0)
     {
         System.out.print(n);
         numbers(n-2);
         System.out.print(n);
     }
 }

I have the above code snippet.我有上面的代码片段。 When i call numbers(5) It gives me an output : 531135当我拨打 numbers(5) 时,它给了我一个输出:531135

I understood till 531. But after this how can the second print statement be even reached when n<0??我理解到 531。但是在此之后,当 n<0 时,如何才能达到第二个打印语句?? And why is it 135 like this?为什么是这样的135?

I really don't have any clue regarding this and please help me with a detailed explanation and dry run.我真的对此一无所知,请帮助我进行详细的解释和试运行。

If I am asking something silly please forgive me.如果我问一些愚蠢的事情,请原谅我。

Thank you谢谢

I'm gonna try to trace what the program is doing:我将尝试跟踪程序在做什么:

numbers(5)
  5>0
    print(5)
    numbers(3)
      3>0
        print(3)
        numbers(1)
          1>0
            print(1)
            numbers(-1)
              -1>0
            print(1)
        print(3)
    print(5)

As you can see, the second print -statement of each function call does not depend on any if (the if of the recursive call doesn't matter to the calling method).如您所见,每个函数调用的第二个print语句不依赖于任何if (递归调用的if与调用方法无关)。 Therefore every number is ALWAYS printed twice (or never).因此,每个数字总是打印两次(或从不打印)。

I hope this is somewhat clear to understand.我希望这有点清楚理解。

You can use a debugger to walk through the program.您可以使用调试器来遍历程序。 Below I modified your program and added a few println() showing the various states of the program.下面我修改了你的程序并添加了一些println()显示程序的各种状态。 I also added r to show the recursion level and __ to help indenting the output.我还添加了r来显示递归级别和__来帮助缩进输出。 See if this now help you understand better.看看这是否能帮助您更好地理解。

public class NumberRecursion {

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

    }

    private static int r = 0;           // recursion level
    private static String __ = "";      // indent prefix
    public static void numbers(int n)
     {
        r++;                            // recursion level
        __ += "    ";                   // indent by another four spaces 

        System.out.println(__+ "[" + r + "] numbers(" + n + ") entered");
         if(n>0)                    // L3
         {
             System.out.println(__+ "["  + r + "] L3 tested true (and n=" + n + ")");
             System.out.println(n); // P1
             System.out.println(__+ "["  + r + "] about to invoke numbers(" + (n-2) + ")");
             numbers(n-2);          // N
             System.out.println(__+ "["  + r + "] numbers(" + (n-2) + ") just finished");
             System.out.println(n); // P2
         } else {
            System.out.println(__+ "[" + r + "] L3 tested false (and n=" + n + ")");
         }
        System.out.println(__+ "["  + r + "] numbers(" + n + ") about to return");
        __ = __.substring(0, __.length()-4);
                                        // indent now has four less spaces
        r--;                            // recursion level
     }


}

The output of this program would be this.这个程序的输出是这样的。 In this output, the number in square brackets (eg [1] ) is the level of recursion.在此输出中,方括号中的数字(例如[1] )是递归级别。

    [1] numbers(5) entered
    [1] L3 tested true (and n=5)
5
    [1] about to invoke numbers(3)
        [2] numbers(3) entered
        [2] L3 tested true (and n=3)
3
        [2] about to invoke numbers(1)
            [3] numbers(1) entered
            [3] L3 tested true (and n=1)
1
            [3] about to invoke numbers(-1)
                [4] numbers(-1) entered
                [4] L3 tested false (and n=-1)
                [4] numbers(-1) about to return
            [3] numbers(-1) just finished
1
            [3] numbers(1) about to return
        [2] numbers(1) just finished
3
        [2] numbers(3) about to return
    [1] numbers(3) just finished
5
    [1] numbers(5) about to return

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

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