// 计算n的阶乘
public class test {
public static void main(String[] args) {
int n = 15;
int result = factorial(n);
System.out.println(result);
}
public static int factorial(int n){
if (n == 1){ //这里要进行判断一下,当n=1时,返回1;
return 1;
}
int i = n * factorial(n - 1); //自己调用自己,并将值传给i
return i;
}
}
结果
2004310016
Process finished with exit code 0