/** * 计算Factorial阶乘! * @param n 任意大于等于0的int * @return n!的值 */ public static BigInteger getFactorial(int n) { if (n < 0) { System.err.println("n必须大于等于0!"); return new BigInteger("-1"); } else if (n == 0) { return new BigInteger("0"); } //将数组换成字符串后构造BigInteger BigInteger result = new BigInteger("1"); for (; n > 0; n--) { //将数字n转换成字符串后,再构造一个BigInteger对象,与现有结果做乘法 result = result.multiply(new BigInteger(new Integer(n).toString())); } return result; }