1
Ich soll eine Tabelle der Ganzzahlen von 0 bis 30 zusammen mit ihren Fakultäten drucken. Ich habe es versucht, aber ich bekomme immer wieder eine Fehlermeldung, dass BigInteger (long) privaten Zugriff in BigInteger hat? Gedanken?BigInteger Factorial Tabelle 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);
}
Wenn ich dies tue sagt es, dass die Klasse valueOf nicht gefunden werden kann ... –
nahm die neue und fügte hinzu, Ihren Vorschlag und es funktioniert! Vielen Dank! –
Kein Problem .. Froh, dass ich helfen könnte. –