递归原理示意图

递归示例代码:

package com.zyy.recursion;

public class RecursionTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //  通过打印问题,回顾递归调用机制
        test(4);

        int res = factorial(3);
        System.out.println("res=" + res);
    }

    // 打印问题.
    public static void test(int n) {
        if( n > 2 ){
            test(n - 1);
        }
            System.out.println("n = " + n );

    }

    // 阶乘问题
    public static int factorial(int n){
        if (n == 1){
            return 1;
        } else {
            return factorial(n - 1) * n;
        }
    }
}

作者 张, 宴银

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注