Blender3D
Top Contributor
2. Beim Umstellen des Codes auf BigInteger habe ich Probleme. Abhilfe täte gut. Kannst Du mir einen Tipp geben. Ich bekomme lauter Fehlermeldungen. Das sind zu viele um Sie hier zu posten.
Java:
import java.math.BigInteger;
import java.util.Scanner;
public class StartCombineSymbols {
private final static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
String symbols = "012"; // definiert die verwendeten Symbole
String label = "Bitte die Anzahl der Stellen\nfuer die Kombinationen\nder Symbole ( " + symbols
+ " ) eingeben?\n";
int numDigits = inputInt(label, 2, 10); // liest ein Zahl von 2 - 10 ein
BigInteger numCombination = new BigInteger(Integer.toString((int) Math.pow(symbols.length(), numDigits)));
System.out.println("Anzahl der Kombinationen = " + numCombination);
for (BigInteger num = BigInteger.ZERO; num.compareTo(numCombination) < 0; num = num.add(BigInteger.ONE))
System.out.println(convertIntDigitsToSymbols(num, symbols.toCharArray(), numDigits));
}
public static String convertIntDigitsToSymbols(BigInteger number, char[] symbols, int minLenght) {
StringBuilder result = new StringBuilder();
BigInteger base = new BigInteger(Integer.toString(symbols.length));
while (minLenght-- > 0 || !number.equals(BigInteger.ZERO)) {
result.append(symbols[number.mod(base).intValue()]);
number = number.divide(base);
}
return result.reverse().toString();
}
private static int inputInt(String label, int min, int max) {
boolean ok = false;
int value = 0;
do {
System.out.print(label + "-> ");
try {
value = Integer.parseInt(input.next());
if (value < min || value > max)
System.out.println("Wert muss im Bereich [" + min + "-" + max + "] liegen");
else
ok = true;
} catch (NumberFormatException e) {
System.out.println("Bitte eine Zahl eingeben!");
}
} while (!ok);
return value;
}
}