利用控制台输入一个数计算这个数的阶乘
例如:5的阶乘为 5 * 4 * 3 * 2 * 1
import java.util.Scanner;
class Demo01
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要阶乘的数:");
int n = sc.nextInt();
System.out.println(n + "的阶乘为:" + sum(n));
}
public static int sum(int n) {
if (n == 1) {
return 1;
}
return n * sum(n - 1);
}
}