方法:递归讲解

方法:递归讲解

package com.andy.base.Andy.operator.method;

public class Demo05 {
    public static void main(String[] args) {

        //递归头:什么时候不调用自身方法,如果没有头,将陷入死循环
        //递归体:什么时候需要调用自身方法
        Demo05 test = new Demo05();
        test.test();
        //这就报错了

    }

    public void test(){
        test();
}

}

package com.andy.base.Andy.operator.method;

public class Demo06 {
    public static void main(String[] args) {

        System.out.println(f(5));
    }
    //5! 5*4*3*2*1  递归
    public static  int f(int n ){
        if(n==1){
            return 1;
        }else {
            return n*f(n-1);
        }
    }
}

posted @ 2023-02-26 19:18  努力学习的J1an-JIan  阅读(20)  评论(0)    收藏  举报