我想知道爲什麼代碼的頂層代碼執行但代碼的底部代碼塊沒有。我所做的一切讓對方執行了切換在我的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