采用刚才的原则做指导,我们写阶乘递归就非常简单了,公式n!=n*(n-1)!
代码语言:javascript复制import java.util.Scanner;
/**
* Created by Administrator on 2018-02-15.
*/
public class Fact {
public static int FactQty(int n) {
if(n == 1) {
return 1;
}else {
return n * FactQty(n - 1);
}
}
public static void main(String[] args) {
System.out.print("输入阶乘的数值:");
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int num = Fact.FactQty(n);
System.out.println(n "的阶乘为" num);
}
}