我正在尝试编写一个 Java 方法,该方法使用计数顺序排序 n 个不同对象的方法数量 - 排列。每次我尝试编译代码时,都会收到一条错误消息:
multiply(long) is not public in BigInteger; cannot be accessed from outside package.
我尝试将行 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;
}
}
最佳答案
要乘以 BigInteger
,您需要提供 BigInteger 参数,而不是 long 参数。方法是BigInteger.multiply(BigInteger)
.
将代码更改为:
fact = fact.multiply(BigInteger.valueOf(i));
附注:
BigInteger.valueOf((long) 1);
应替换为BigInteger.ONE
。已经有一个预定义的常数。- 请务必遵守 Java 命名约定:
CountPerm
方法应称为countPerm
。
关于java - 错误: multiply(long) is not public in BigInteger; cannot be accessed from outside package,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32809422/