1.利用递归方式计算阶乘
public static long factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
2./利用迭代方式计算阶乘
public static long factorial2(int n) {
int r = 1;
for (int i = 1; i <= n; i++) {
//r = r * i;
r *= i;
}
return r;
}