Java Recursion in an If/Else statement -
i'm trying figure out why top block of code executes bottom block of code not. did other execute switch conditions position in if/else statement
public static void onthewall(int bottles){ if (bottles == 0){ system.out.println("no bottles of beer on wall," + " no bottles of beer, ya’ can’t take 1 down, ya’ can’t pass around," + "cause there no more bottles of beer on wall!"); } else if (bottles <= 99){ system.out.println(bottles + " bottles of beer on wall, " + bottles + " bottles of beer, ya’ take one" + " down, ya’ pass around, " + (bottles - 1) + " bottles of beer on wall"); onthewall(bottles-1); } } public static void onthewall(int bottles){ if (bottles <= 99){ system.out.println(bottles + " bottles of beer on wall, " + bottles + " bottles of beer, ya’ take one" + " down, ya’ pass around, " + (bottles - 1) + " bottles of beer on wall"); onthewall(bottles-1); } else if (bottles == 0){ system.out.println("no bottles of beer on wall," + " no bottles of beer, ya’ can’t take 1 down, ya’ can’t pass around," + "cause there no more bottles of beer on wall!"); } }
your switching conditions if (bottles <= 99) , else if (bottles == 0) causes first block execute because first condition (bottles <= 99) true bottles=0.
if expecting else if execute before if statement, never going happen.
maybe condition should if (bottles > 0 && bottles <= 99) in case second block execute if bottles=0 expect to.
Comments
Post a Comment