2016-03-31 31 views
1

Tam sayıları içeren bir tabloyu fakülteyle birlikte 0'dan 30'a yazdırmam gerekiyor. Denedim, ama BigInteger (uzun) BigInteger'de özel erişimi olduğunu söyleyerek bir hata almaya devam ediyorum? Düşünceler?BigInteger Factorial Table 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); 
} 

cevap

1

bir long alarak bir BigInteger yapıcı yok, ama o kadar sadece BigInteger sınıftan kendisinden çağrılabilir private olduğunu.

Bunun yerine BigInteger.valueOf(n) kullanmanız gerekir.

+0

bunu yaptığımda, classOfff bulunamıyor ... –

+0

yeni çıktı ve önerinizi ekledi ve işe yarıyor! Çok teşekkürler! –

+0

Sorun değil .. Sevindim. –

İlgili konular