Java 跟踪递归方法

Java 跟踪递归方法,java,recursion,Java,Recursion,我发现理解递归的概念非常混乱。我试图跟踪一个递归函数。有人能帮我吗 public static int h(int n){ if (n == 0) return 0; else return h(n-1)+1; } 当我写作时 int a = h(5); System.out.println(a) 我不明白生成的结果实际上是如何产生的?要在调试器中跟踪此递归调用,请在if语句上设置断点,然后运行程

我发现理解递归的概念非常混乱。我试图跟踪一个递归函数。有人能帮我吗

    public static int h(int n){
        if (n == 0)
            return 0;
        else
            return h(n-1)+1;
    }
当我写作时

int a = h(5);
System.out.println(a)

我不明白生成的结果实际上是如何产生的?

要在调试器中跟踪此递归调用,请在
if
语句上设置断点,然后运行程序。到达断点时:

  • 检查
    n
    的值
  • 查看调用堆栈窗口

调用堆栈上的项目数将随着每次递归调用而增加;
n
的值将下降1。当您深入到调用的多个级别时,单击调用堆栈上的不同项。它会将您带到呼叫站点(即
返回h(n-1)+1
)。您将能够在此堆栈级别检查
n
的值。

尝试记录。或者,只需调试打印:

public static int h(int n){
    System.out.println("called h(" + n + ")");
    if (n == 0) {
        System.out.println("we know the result for 0, returning 0");
        return 0;
    } else {
        System.out.println("we don't know the result, calling for " + (n-1));
        int t = h(n-1);
        System.out.println("Found the result for " + (n-1) + 
                           ", calculating the result for " + n);
        return t + 1;
    }
}
对于
n=4
,您将得到:

called h(4)
we don't know the result, calling for 3
called h(3)
we don't know the result, calling for 2
called h(2)
we don't know the result, calling for 1
called h(1)
we don't know the result, calling for 0
called h(0)
we know the result for 0, returning 0
Found the result for 0, calculating the result for 1
Found the result for 1, calculating the result for 2
Found the result for 2, calculating the result for 3
Found the result for 3, calculating the result for 4
希望它能给你一个线索-玩不同的算法,看看会发生什么


另外,试着打电话给
h(-1)
——玩得开心

首先,如果您在理解递归概念方面有困难,我认为以下链接将帮助您:

您可以使用IDE上的调试工具来查看它是如何工作的。您可以通过谷歌搜索有关如何设置点的说明,并使用调试器逐步完成程序

关于方法
h
,它将返回您作为输入提供的内容(如果是正数或0)。此外,较大的数字和负数将导致堆栈溢出错误。要了解工作原理,可以在方法中使用print语句

public static int h(int n) {
    System.out.println("h(" + n + ")");
    if (n == 0) {
        System.out.println("value: 0");
        return 0;
    } else {
        System.out.println("going down");
        int temp = h(n - 1) + 1;
        System.out.println("h(" + n + ") --> " + temp);
        return temp;
    }
}
将输出:

h(5)
going down
h(4)
going down
h(3)
going down
h(2)
going down
h(1)
going down
h(0)
value: 0
h(1) --> 1
h(2) --> 2
h(3) --> 3
h(4) --> 4
h(5) --> 5
可以编辑上述输出以显示工作状态:

h(5)
|    going down
|----h(4)
|    |   going down
|    |---h(3)
|    |   |   going down
|    |   |---h(2)
|    |   |   |  going down
|    |   |   |--h(1)
|    |   |   |  |    going down
|    |   |   |  |----h(0)
|    |   |   |  |    |    value: 0 --> return 0;
|    |   |   |  |    h(1) --> 1 --> h(0) + 1 = 0 + 1 = 1
|    |   |   |  h(2) --> 2          h(1) + 1 = 1 + 1 = 2
|    |   |   h(3) --> 3             h(2) + 2 = 1 + 1 = 3
|    |   h(4) --> 4                 h(3) + 3 = 1 + 1 = 4
|    h(5) --> 5                     h(4) + 4 = 1 + 1 = 5
以下是方法
h
的非递归版本

public static int nonh(int n) {
    int result = 0;
    for (int i = n; i > 0; i--) {
        result += 1;
    }

    return result;
}

希望有帮助:)

你说的“追踪”是什么意思?如果您将System.out.println(“h(“+n+”)”);在if(n==0)之前,您希望h(5)的输出是什么?正如您所说,这是一个简单的递归方法。你在这里困惑的根源到底是什么?特别提到,你到底想追踪什么?这满足递归函数的所有3个条件。如果它只是关于递归的基本概念,您可能想从开始。这使它非常清楚。但当我做int a=h(5);System.out.println(a)为什么它返回5?那是因为递归方法返回传递给它的值:h(0)返回0,h(1)返回0+1,h(2)返回0+1+1,等等。你能给我推荐这个方法的非递归版本吗。也许这会帮助我更好地理解它。直到值为0时我才明白,那么其他行是如何打印的?