2015-03-30 63 views
0

我試圖打印歌詞到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; 
    } 
} 
+0

是你嘗試使用迭代或遞歸?它看起來像你基本上困惑,並試圖使用兩者。 – Radiodef 2015-03-30 19:13:04

+0

我正在試圖遞歸 – pati3ntzero 2015-03-30 19:29:12

+0

只是給你一個提示:你的方法將遵循像靜態無效瓶(INT B){如果(B> 0)瓶(B - 1); }'這將倒數爲0. – Radiodef 2015-03-30 19:37:17

回答

0

看看你bottlesOfBeer(int beer)方法 -

public static void bottlesOfBeer(int beer) 

它的返回類型爲void。你在for循環中使用它作爲佔位符遞增/遞減

public static void bottlesOfBeer(int beer)返回一些int這個方法。

希望它會有所幫助。
謝謝。

0

好的,問題是你從來沒有真正減少啤酒。正確的方法是用beer--替換bottlesOfBeer(beer-1)部分。這會在每次循環運行時自動減少啤酒。

這也意味着可以刪除if(beer == 99)檢查。如果您始終希望它從99開始,請創建一個名爲BEERS_START = 99的常量並使用它來初始化啤酒。如果您希望它能夠從任意整數開始,那麼只需刪除該檢查。

編輯: 鑑於你的老師需要遞歸,那麼你應該以不同的方式處理遞歸。我不會提供代碼,因爲我無法提供一個很好的示例,而無需爲您編寫任務。但是,做這件事比你做的要簡單得多。在這種方法中你根本不需要for循環。對於這個問題,你也不需要if語句。嘗試設計不帶for循環的遞歸,並且不使用if語句。一旦你到達那裏,最終的結果將會簡單得多。

如果您在嘗試之後仍然遇到問題,請在問題中發佈一些更新的代碼,其中包含您的新嘗試,並在另一條評論中告知我。我會看看我能否從那裏幫助。

+0

這就是我最初的做法,然而,當我將它提交給老師(是的,我是一個學習Java課程的學生)時,老師說「bottlesOfBeer」方法需要調用自己。 – pati3ntzero 2015-03-30 19:23:19

+0

@ pati3ntzero好吧,考慮到你老師的任意要求,我在答案中提供了一些更多的指導。我不能給你代碼,因爲我基本上會給你答案,但希望它能指出你正確的方向。 – 2015-03-30 19:32:55

1

我不認爲這是做你認爲它在做什麼。有遞歸它只能走一層。當您撥打bottlesOfBeer(beer - 1)時,此遞歸調用將保證在該方法開始時由於if (beer == 99)而不執行任何操作。

如果beer == 99因此只會輸入if語句,因此當您再次調用方法beer - 1時,它將會失敗if語句和結束遞歸。

你們看到的是一個無限循環(不是無限遞歸不同),因爲在你的for循環沒有在那裏是beer本地副本遞減。因此,for循環中的beer將始終爲99,因此for循環將永遠運行。

你可能想是這樣的:

public static void bottlesOfBeer(int beer) { 
    if (beer > 1) { 
     System.out.println(beer 
       + " bottles of Beer on the wall!" 
       + beer + " bottles of Beer!" 
       + " Take one down, pass it around, " 
       + (beer - 1) + " bottles of beer on the wall!"); 
     bottlesOfBeer(beer - 1); 
    } 
    else if (beer == 1) { 
     System.out.println(beer 
       + " bottle of Beer on the wall!" 
       + beer + " bottle of Beer!" 
       + " Take one down, pass it around, " 
       + " no more bottles of beer on the wall!"); 
    } 
    else { 
     // Do nothing if beer <= 0 
    } 
} 
+0

'else if(啤酒== 0){System.out.println('去商店買更多'); }'? – Aemyl 2016-11-11 12:56:01

+0

不確定你在引用什麼。 – curob 2016-11-11 19:47:12

2

你們是不是一個真正的遞歸的方法解決問題。遞歸通常會執行而不是循環。你有一個奇怪的遞歸和迭代混搭。

如果你的要求是通過遞歸來解決問題,那麼就要弄清楚如何在沒有任何循環結構的情況下做到這一點(不,我不會爲你做功課)。你實際上已經擁有了大部分你需要的東西。

0

試試這個

public class HelloWorld { 
public static void main(String[] args) { 
    beers(3); 

} 

public static void beers(int n) { 
    if (n == 0){ 
     System.out.println(); 
     System.out.println("No bottles of beer on the wall"); 
     System.out.println("no bottles of beer,"); 
     System.out.println("ya’ can’t take one down, ya’ can’t pass it around,"); 
     System.out.println("’cause there are no more bottles of beer on the wall!"); 
    } 
    else{ 
     System.out.println(n + " bottles of beer on the wall"); 
     System.out.println(n + " bottles of beer"); 
     System.out.println("Ya drink one down, pass it around,"); 
     System.out.println((n-1) + " bottles of beer on the wall"); 
     System.out.println(); 

     beers(n - 1); 

    } 
} 
}