No no no. This is definitely less mysterious than necessary. Go with my decision;)
public class Main { static String btl(int i) { return i > 1 ? " bottles of beer" : " bottle of beer"; } public static void main(String [] arg) { String [] btls = { " on the wall, ", "\nTake one down, pass it round, " }; for(int i=99;i>0;i--) System.out.println("" + i + btl(i) + btls[0] + i + btl(i) + btls[1] + ((i>1)?i+btl(i):"no more bottles of beer")); } }
and it works like a charm;)
.... 4 bottles of beer on the wall, 4 bottles of beer Take one down, pass it round, 4 bottles of beer 3 bottles of beer on the wall, 3 bottles of beer Take one down, pass it round, 3 bottles of beer 2 bottles of beer on the wall, 2 bottles of beer Take one down, pass it round, 2 bottles of beer 1 bottle of beer on the wall, 1 bottle of beer Take one down, pass it round, no more bottles of beer
And you can always go with recursive calls;)
public class Main { static String [] btls = { " on the wall, ", "\nTake one down, pass it round, " }; static String btl(int i) { return i > 1 ? " bottles of beer" : " bottle of beer";} static int drink(int i) { System.out.println(""+i+btl(i)+btls[0]+i+btl(i)+btls[1]+((i>1)?i+btl(i):"no more bottles of beer")); return (i>1)?drink(i-1):0; } public static void main(String [] arg) { drink(99); } }
mko
source share