我做了一个递归方法来计算阶乘,但在主要方法中我使用了一个 for 循环来计算阶乘列表。有没有办法在 main 方法中不使用循环来计算阶乘列表?
代码:
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);
}
}