作业帮 > 综合 > 作业

java 数 n的阶乘, 阶乘, 公式 为 n!=n*(n-1)(n-2)…*2 * 1 .求 数字 6的阶乘 的阶乘

来源:学生作业帮 编辑:拍题作业网作业帮 分类:综合作业 时间:2024/04/27 22:06:57
java 数 n的阶乘, 阶乘, 公式 为 n!=n*(n-1)(n-2)…*2 * 1 .求 数字 6的阶乘 的阶乘 .
java编程
import java.math.BigInteger;
 
public class Test {
 
    public static void main(String[] args) {
        int n = 6;
        System.out.println(jiecheng(n));
    }
    
    public static BigInteger jiecheng (int i) {
         BigInteger result = new BigInteger("1");
        for (int j = 1; j <= i; j ++) {
               BigInteger num = new BigInteger(String.valueOf(j));
               result = result.multiply(num);
        }
        return result;
    }
再问: 是错的
再答:  public class Test {
 
    public static void main(String[] args) {
        int n = 6;
        System.out.println(jiecheng(n));
    }
    
    public static Double jiecheng (int i) {
        Double result = 1.0;
        for (int j = 1; j <= i; j ++) {
        \x09result *= j;
        }
        return result;
    }
} 第一次回答那个代码只是少了一个“{”号而已,我复制过去的时候不小心。