> 其他 >
java 数 n的阶乘, 阶乘, 公式 为 n!=n*(n-1)(n-2)…*2 * 1 .求 数字 6的阶乘 的阶乘 .
java编程
人气:453 ℃ 时间:2019-12-13 18:04:29
解答
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;
    }
}

 

第一次回答那个代码只是少了一个“{”号而已,我复制过去的时候不小心。

推荐
猜你喜欢
© 2024 79432.Com All Rights Reserved.
电脑版|手机版