05递归(面试常问)

A方法调用A方法,自己调用自己,没有头会陷入死循环。能不用递归就不用递归。

public class Demo03 {
    public static void main(String[] args) {
        System.out.println(f(4));
    }
    //阶层
    //1! 1
    //2! 2*1 2*f(1)
    //5! 5*4*3*2*1
    public static int f(int n){
        if(n==1){
            return 1;
        }else{
            return n*f(n-1);
        }

    }
}
运行结果:24

posted @ 2021-04-17 21:08  且听_198  阅读(35)  评论(0)    收藏  举报