无递归头的递归(会陷入死循环,栈溢出 ):
public class Demo05 {
public static void main(String[] args) {
Demo05 demo05 = new Demo05();
demo05.test();
}
public void test() {
System.out.print("从前有座山,山上有座庙,庙里有个老和尚对小和尚说:");
test();
}
}
输出异常
正常的递归:
public class Demo06 {
//5! 5*4*3*2*1
public static void main(String[] args) {
System.out.println(f(5));
}
public static int f(int n) {
return n == 1? 1: n * f(n-1);//return 1时为边界条件
}
}
输出
120
原文:https://www.cnblogs.com/dt746294093/p/14614469.html