好的,所以我有一个程序:
public class Rec {
public static void main(String[] args) {
test(5);
}
static void test(int n) {
if (n > 0) {
System.out.println(n);
test(n-1);
System.out.println(n);
}
}
它的输出是5,4,3,2,1,1,2,3,4,5。我的问题是,为什么/如何执行第二个 println(n) 语句?我以为函数调用会完全切断它,但它的行为方式却让我感到困惑。这不是家庭作业或任何东西,我只是很难理解递归是如何工作的。
所有方法调用完成后都会返回到同一位置。
通过有效地链接它们,你会得到
System.out.println(5)
System.out.println(4)
System.out.println(3)
System.out.println(2)
system.out.println(1)
// If is not true now.
System.out.println(1)
System.out.println(2)
System.out.println(3)
System.out.println(4)
System.out.println(5)
那有意义吗?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)