zcq

    1. package com.atguigu.recursion;
    2. public class RecursionTest {
    3. public static void main(String[] args) {
    4. // TODO Auto-generated method stub
    5. //通过打印问题,回顾递归调用机制
    6. test(4);
    7. //int res = factorial(3);
    8. //System.out.println("res=" + res);
    9. }
    10. //打印问题.
    11. public static void test(int n) {
    12. if (n > 2) {
    13. test(n - 1);
    14. } //else {
    15. System.out.println("n=" + n);
    16. // }
    17. }
    18. //阶乘问题
    19. public static int factorial(int n) {
    20. if (n == 1) {
    21. return 1;
    22. } else {
    23. return factorial(n - 1) * n; // 1 * 2 * 3
    24. }
    25. }
    26. }