简体   繁体   中英

error: multiply(long) is not public in BigInteger; cannot be accessed from outside package

I'm trying to write a Java method that uses counts the number of ways to sequentially order n distinct objects - a permutation. Every time I try to complile my code, I get an error saying:

multiply(long) is not public in BigInteger; cannot be accessed from outside package.

I tried replacing the line fact = fact.multiply(i); with fact = fact.multiply((long) i); which didn't work either. Does anyone have any ideas?

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;
    }
}

To multiply BigInteger s, you need give a BigInteger parameter, not a long parameter. The method is BigInteger.multiply(BigInteger) .

Change your code to:

fact = fact.multiply(BigInteger.valueOf(i));

As a side note:

  • BigInteger.valueOf((long) 1); should be replaced by BigInteger.ONE . There is already a predefined constant for one.
  • Be sure to respect Java naming conventions: CountPerm method should be called countPerm .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM