error: multiply(long) is not public in BigInteger; cannot be accessed from outside package
error: multiply(long) is not public in BigInteger; cannot be accessed from outside package
我正在尝试编写一个 Java 方法,该方法使用对 n 个不同对象按顺序排序的方法的数量进行计数 - 一种排列。每次我尝试编译我的代码时,我都会收到一条错误消息:
multiply(long) is not public in BigInteger; cannot be accessed from outside package.
我尝试用 fact = fact.multiply((long) i);
替换 fact = fact.multiply(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
s,你需要给一个 BigInteger 参数,而不是 long 参数。方法是BigInteger.multiply(BigInteger)
.
将您的代码更改为:
fact = fact.multiply(BigInteger.valueOf(i));
旁注:
BigInteger.valueOf((long) 1);
应替换为 BigInteger.ONE
。已经有一个预定义的常量。
- 一定要遵守 Java 命名约定:
CountPerm
方法应该被称为 countPerm
.
我正在尝试编写一个 Java 方法,该方法使用对 n 个不同对象按顺序排序的方法的数量进行计数 - 一种排列。每次我尝试编译我的代码时,我都会收到一条错误消息:
multiply(long) is not public in BigInteger; cannot be accessed from outside package.
我尝试用 fact = fact.multiply((long) i);
替换 fact = fact.multiply(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
s,你需要给一个 BigInteger 参数,而不是 long 参数。方法是BigInteger.multiply(BigInteger)
.
将您的代码更改为:
fact = fact.multiply(BigInteger.valueOf(i));
旁注:
BigInteger.valueOf((long) 1);
应替换为BigInteger.ONE
。已经有一个预定义的常量。- 一定要遵守 Java 命名约定:
CountPerm
方法应该被称为countPerm
.