代码:
public static void main(String[] args) {
System.out.println(test(13549));
}
public static int test(int a){
if(a<10)
return a;
int b = (a%10);
int c = test(a/10);
int d = Math.max(b,c);
return d;
}
我了解该方法的作用(使用调试器后),并且我了解该方法会调用自身,直到它小于 10,然后它会运行并检查 b 或 c 中哪个更大。
现在我不明白的是,为什么当有 return 语句 return d;
时,它返回到 int c = test(a/10)
而不是开始int test(int a){
方法。
最佳答案
return
语句返回test
调用的输出。因此,在 return d;
行上,它只是返回 c = test(a/10)
行中 test(a/10)
的值>.
关于java - Java 中的递归,当方法调用自身之后有 return 语句时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15487427/