我正在尝试编写一个Java方法,该方法使用计数顺序排列n个不同对象的方式的数量 - 排列。每次我尝试编译我的代码时,都会收到错误消息:
在BigInteger中,multiply(long)不公开;无法从外包装进入。
我尝试将行fact = fact.multiply(i);
替换为fact = fact.multiply((long) i);
,这也无法正常工作。有没有人有任何想法?
import java.math.BigInteger;
public class Combinatorics {
public static void main(String[] args) {
// 2.1
System.out.println(CountPerm(9));
}
public static BigInteger CountPerm(int n) {
BigInteger fact = BigInteger.valueOf((long) 1);
for(int i = 1; i <= n; i++){
fact = fact.multiply(i);
}
return fact;
}
}
答案 0 :(得分:2)
要乘以BigInteger
s,您需要提供BigInteger参数,而不是long参数。方法是BigInteger.multiply(BigInteger)
。
将您的代码更改为:
fact = fact.multiply(BigInteger.valueOf(i));
作为旁注:
BigInteger.valueOf((long) 1);
应替换为BigInteger.ONE
。已有一个预定义的常量。CountPerm
方法应调用countPerm
。