import java.util.Scanner;
public class Factorial{
public static int factorial(int n){
if(n==1){
return 1;
}
else{
return n*factorial(n-1);
}
}
public static void main(String[] args){
System.out.println("请输入要求n的阶乘,n的值:");
Scanner s=new Scanner(System.in);
int n=s.nextInt();
System.out.println(factorial(n));
}
}
[Java]递归算法求n的阶乘(n>1,n是正整数)
最新推荐文章于 2023-01-23 09:21:48 发布