我想知道为什么代码的顶层代码执行但代码的底部代码块没有。我所做的一切让对方执行了切换在我的if/else语句的条件位置Java在If/Else语句中的递归
public static void onTheWall(int bottles){
if (bottles == 0){
System.out.println("No bottles of beer on the wall,"
+ " no bottles of beer, ya’ can’t take one down, ya’ can’t pass it around,"
+ "cause there are no more bottles of beer on the wall!");
} else if (bottles <= 99){
System.out.println(bottles + " bottles of beer on the wall, "
+ bottles + " bottles of beer, ya’ take one"
+ " down, ya’ pass it around, "
+ (bottles - 1) + " bottles of beer on the wall");
onTheWall(bottles-1);
}
}
public static void onTheWall(int bottles){
if (bottles <= 99){
System.out.println(bottles + " bottles of beer on the wall, "
+ bottles + " bottles of beer, ya’ take one"
+ " down, ya’ pass it around, " + (bottles - 1)
+ " bottles of beer on the wall");
onTheWall(bottles-1);
} else if (bottles == 0){
System.out.println("No bottles of beer on the wall,"
+ " no bottles of beer, ya’ can’t take one down, ya’ can’t pass it around,"
+ "cause there are no more bottles of beer on the wall!");
}
}
顶一个应该工作,底部将始终执行第一个if语句 – JRowan
零不到99. –
谢谢@JRowan你会碰巧知道它为什么会继续执行第一个if语句吗?如果不是,谢谢你,我会做更多的研究。 – RustyShackleford