gpt4 book ai didi

java - 简单递归

转载 作者:行者123 更新时间:2023-12-02 05:51:01 25 4
gpt4 key购买 nike

我在玩这个函数:

public class x {

public static void main(String[] args) {
recurse(10);
}

public static int recurse(int theNumber) {

if(theNumber == 0) {
return 0;
}

else
{
System.out.println(theNumber);
theNumber--;
recurse(theNumber);
System.out.println(theNumber);
}

return -1;
}
}

我得到了这个输出:

1098个76个5个4个3个2个1个01个2个3个4个5个6个78个9按任意键继续 。 . .

这怎么可能?我知道从 10 到 0 的倒计时是从哪里来的……但是它到底是怎么倒计时的?我很确定我错过了关于递归的基本概念。有人可以填空吗??

最佳答案

那是因为你方法中的最后一个 System.out.println(theNumber);。在递归调用该方法后,upcon 完成该方法,它正在打印这些值。

关于java - 简单递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15758096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com