我試圖打印歌詞到99瓶啤酒,但是,我得到了無限的遞歸,設置在第一節。關於如何擺脫這種無限遞歸的任何想法?99瓶啤酒的無限遞歸
public static void bottlesOfBeer(int beer) { //prints the lyrics for "99 bottles of Beer on the wall".
if (beer == 99) {
for (beer = 99; beer > 0; bottlesOfBeer(beer - 1)) {
System.out.println(beer
+ " bottles of Beer on the wall!"
+ beer + " bottles of Beer!"
+ " Take one down, pass it around, "
+ minusOneBeer(beer) + " bottles of beer on the wall!");
}
}
}
public static int minusOneBeer(int beer) {
return beer - 1;
}
}
是你嘗試使用迭代或遞歸?它看起來像你基本上困惑,並試圖使用兩者。 – Radiodef 2015-03-30 19:13:04
我正在試圖遞歸 – pati3ntzero 2015-03-30 19:29:12
只是給你一個提示:你的方法將遵循像靜態無效瓶(INT B){如果(B> 0)瓶(B - 1); }'這將倒數爲0. – Radiodef 2015-03-30 19:37:17