Maybe you could use a recursive algorithm:
public void printBin(String soFar, int iterations) {
if(iterations == 0) {
System.out.println(soFar);
}
else {
printBin(soFar + "0", iterations - 1);
printBin(soFar + "1", iterations - 1);
}
}
You would execute this like this:
printBin("", 4);
That would give you all possible binary numbers with 4 digits.
Hope this helped!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…