簡體   English   中英

調用帶有BigInteger參數的方法時遇到問題

[英]Problems invoking a method with a BigInteger parameter

我遇到了麻煩,對此我無能為力。 我想知道如何調用此方法?

public static BigInteger factorial(BigInteger n) {
    BigInteger result = BigInteger.ONE;

    while (!n.equals(BigInteger.ZERO)) {
        result = result.multiply(n);
        n = n.subtract(BigInteger.ONE);
    }

    return result;
}

對於var = 1,我明白了,我寫道:

BigInteger kk = BigInteger.ONE;
System.out.println(factorial(kk));

但是,例如,我很困惑如何計算61!。

嘗試:

 BigInteger kk = new BigInteger("61");
 System.out.println(factorial(kk));

Java也有一個靜態工廠方法

BigInteger kk = BigInteger.valueOf(61L);
BigInteger kk = new BigInteger("61");
System.out.println(factorial(kk));

該API是您的朋友: http : //docs.oracle.com/javase/1.4.2/docs/api/java/math/BigInteger.html

更改

BigInteger kk = BigInteger.ONE
System.out.println(factorial(kk));

BigInteger kk=new BigInteger("61");
System.out.println(factorial(kk));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM