闭着眼睛写阶乘递归 顶

2019-08-20 10:50:42 浏览数 (1)

采用刚才的原则做指导,我们写阶乘递归就非常简单了,公式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);
    }
}

0 人点赞