如何在不使用循环的情况下打印阶乘?

use*_*357 0 java recursion factorial

我做了一个递归方法来计算阶乘,但在主方法中我使用了一个for循环来计算阶乘列表.有没有办法在主方法中不使用循环计算阶乘列表?

码:

public class FactInt {
    public static void main(String[] args) {
        for (int n = 1; n < 11; n++)
            System.out.println(factorial(n));
    }
    //Calculates the factorial of integer n
    public static int factorial(int n) {
        if (n == 0)
            return 1;
        else 
            return n*factorial(n-1);
    }
}
Run Code Online (Sandbox Code Playgroud)

Tim*_* S. 7

它取决于你"计算一个列表"的确切含义,但这打印相同的东西:

public static void main(String[] args) {
    factorial(10);
}
//Calculates the factorial of integer n
public static int factorial(int n) {
    if (n == 0)
        return 1;
    else {
        int newVal = n*factorial(n-1);
        System.out.println(newVal);
        return newVal;
    }
}
Run Code Online (Sandbox Code Playgroud)