1、递归调用:
递归调用就是在当前的函数中调用当前的函数并传给相应的参数,这是一个动作,这一动作是层层进行的,直到满足一般情况的的时候,才停止递归调用,开始从最后一个递归调用返回。
2、递归调用的方法计算4的阶乘为例代码如下:
/**
* 用递归算4的阶乘
*/
public class Factorial {
public static void main(String[] args) {
//对factorial方法进行调用
factorial(4);
}
//定义方法
public static int factorial(int n) {
if (n == 0) {
System.out.println(n + "! = 0");
return 1;
} else {
System.out.println(n);
//用递归的方法进行阶乘处理
int temp = n*factorial(n-1);
System.out.println(n + "! = " + temp);
return temp;
}
}
}
运行结果:
4
3
2
1
0! = 0
1! = 1
2! = 2
3! = 6
4! = 24
3、4的阶乘递归图解如下: