鉴于您的代码是
public static int mystery(int m, int n) {
int result = 1;
if (m > 0) {
result = n * mystery(m-1, n);
}
System.out.println (m + " " + result);
return result;
}
让我们从 m = 3 和 n = 4 开始,让我们尝试通过尝试成为调试器来模拟它...
mystery(3,4){
int result = 1
if(3 > 0){
result = 4 * mystery(3-1,4);
//We proceed from this point only after evaluating mystery(2,4)
mystery(2,4){
int result = 1
if(2 > 0){
result = 4*mystery(2-1,4);
//Now we have to evaluate mystery(1,4)
mystery(1,4){
int result = 1;
if(1 > 0){
result = 4*mystery(1-1,4);
//Evaluate mystery(0,4)
mystery(0,4){
int result = 1;
if(0 > 0){
//Not evaluated
}
System.out.println(0 + " "+1);
return 1;
}...mystery(0,4) done continue with evaluation of mystery(1,4)
result = 4*1 //1 is what is returned by mystery(0,4)
System.out.println(1+ "" + 4);
return 4;
}//done with the evaluation of mystery(1,4), resume evaluation of mystery(2,4)
result = 4*4 //4 is the value returned by mystery(1,4)
System.out.println(2 + " " + 16);
return 16;
}//At this point we are done with evaluating (2,4) and on the way to resume evaluation of mystery(3,4)
result = 4 * 16
System.out.println(3 + " "+ 64)
return 64;
}
}
希望这可以帮助