编辑代码

public class Factorial1 {
    public static void main(String[] args) {
        int n = 5;
        long result = factorialRecursive(n);
        System.out.println("递归算法," + n + "的阶乘是:" + result);
    }

    public static long factorialRecursive(int n) {
        if (n == 0) {
            return 1;
        }
        return n * factorialRecursive(n - 1);
    }
}
// public class Factorial2 {
//     public static void main(String[] args) {
//         int n = 5;
//         long result = factorialIterative(n);
//         System.out.println("递推算法," + n + "的阶乘是:" + result);
//     }

//     public static long factorialIterative(int n) {
//         long result = 1;
//         for (int i = 1; i <= n; i++) {
//             result *= i;
//         }
//         return result;
//     }
// }