计算阶乘数中的零,请检查错误

时间:2018-06-27 17:04:35

标签: java biginteger

编译错误

Main.java:18: error: method mod in class BigInteger cannot be applied to given types;
r = factorialans.mod(10);
                        ^
required: BigInteger
found: int
reason: actual argument int cannot be converted to BigInteger by method invocation conversion
Main.java:22: error: method divide in class BigInteger cannot be applied to given types;
            factorialans = factorialans.divide(10);
                                       ^
  required: BigInteger
  found: int
  reason: actual argument int cannot be converted to BigInteger by method invocation conversion
2 errors

import java.util.Scanner;
import java.math.BigInteger;

class Main  {
    public static void main(String args[]) {
        BigInteger r;
        BigInteger factorialans=new BigInteger("1");

        int n,i,count=0;
        Scanner sc=new Scanner(System.in);
        n = sc.nextInt();
      //factorial...
        for (i = n; i > 0; i--){
            factorialans = factorialans.multiply(BigInteger.valueOf(i));
        }

            System.out.print(factorialans);

        while(!factorialans.equals(0)){
          r = factorialans.mod(10);
            if (r.equals(0)){
                count++;
            }
            factorialans = factorialans.divide(10);
        }
        System.out.print(count);
    }
}

1 个答案:

答案 0 :(得分:1)

请参见doc,应通过以下方式使用它:

public BigInteger mod(BigInteger m)

public BigInteger divide(BigInteger val)

您可以将其更改为此:

factorialans.mod(BigInteger.valueOf(10)) 

factorialans.divide(BigInteger.TEN)