方法一:循环
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入一个正数:");
int n = sc.nextInt();
System.out.println("该数的阶乘是:"+circulation(n));
}
// 循环方法
public static int circulation(int n) {
int result = 1;
for(int i=1;i<=n;i++) {
result*=i;
}
return result;
}
方法二:递归
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入一个正数:");
int n = sc.nextInt();
System.out.println("该数的阶乘是:"+recursion(n));
}
// 递归方法
public static int recursion(int n) {
if(n==1) {
return 1;
}else {
return diGui(n-1)*n;
}
}