Java中的阶乘


/**
 * Description:阶乘 0! = 1,n! = (n-1)! * n;
 */
public class Factorial {

    public static int fac(int n){
        if(n == 0 | n == 1) return 1;
        if(n > 1) return fac(n-1)*n;
        else throw  new IllegalArgumentException("参数错误");
    }
    public static int fact(int n){
        int sum = 1;
        if (n == 0 | n == 1) return sum;
        if (n > 1){
            for (int i = 1; n >= i; i++){
                sum *= i;
            }
        } else throw new IllegalArgumentException("参数错误");
        return sum;
    }

    public static void main(String[] args) {
        System.out.println(fac(6));
        System.out.println(fact(6));
    }
}
原文地址:https://www.cnblogs.com/isxt/p/7995440.html