2016-03-31 7 views
1

私は0から30までの整数の表を階乗と一緒に表示することになっています。私は試みましたが、BigInteger(long)がBigIntegerでプライベートアクセスを持っているとエラーが表示され続けますか?思考?BigInteger Factorial表1-30

public static void main(String[] args) { 
    int x = 0; 
    for (x = 0; x < 31;) { 
     System.out.println(x + " " + factorial(x)); 

     x = x + 1; 

    } 
} 

/* 
     public static int factorial (int n) { 
     if (n == 0) { 
      return 1; 
     } else { 
      return n * factorial (n-1); 
      } 
     } 
     // error occuring at 13! could be because the number becomes too great for int to handle, resulting in an overflow error. 

    }*/ 
public static BigInteger factorial(int n) { 
    if (n == 0) { 
     return BigInteger.ONE; 
    } else { 

     BigInteger result = new BigInteger(n).multiply(factorial(n - 1));(error here) 

     return result; 
    } 
    //return new BigInteger(n) * factorial(n - 1); 
} 

答えて

1

ありlongを取っBigIntegerコンストラクタがあるが、それはprivateあるので、唯一のBigIntegerクラス自体から呼び出すことができます。

代わりにBigInteger.valueOf(n)を使用する必要があります。

+0

私は、クラスvalueOfが見つからないと言っています... –

+0

は新しいものを取り出してあなたの提案を追加しました。本当にありがとう! –

+0

問題ありません。うれしいです。 –