Java

java怎么用递归实现n的阶乘

小亿
84
2024-03-01 11:44:14
栏目: 编程语言

public class Factorial {

    public static int factorial(int n) {
        if (n == 0) {
            return 1;
        } else {
            return n * factorial(n - 1);
        }
    }

    public static void main(String[] args) {
        int n = 5;
        int result = factorial(n);
        System.out.println(n + "! = " + result);
    }
}

0
看了该问题的人还看了