2012-07-26 72 views
3

我正在做什麼應該是一個相對簡單的Java遞歸問題,但我似乎無法找到一個簡單的,單方法的解決方案。遞歸java星號

我試圖以遞減之後升序打印出星號,這樣當用戶在3通過例如,打印出來會是這樣的:

* 
** 
*** 
** 
* 

編輯:謝謝你的幫助的@dasblinkenlight這已演變爲:

public void patternMaker(int start, int max, int direction){ 
    if(start == 0){ 
     return; 
    } 
    for(int i = 0; i < start; i++){ 
     System.out.print("*"); 
    } 
    System.out.println(); 
    if(start == max){ 
     direction = -1; 
    } 
    patternMaker(start + direction, max, direction); 

現在,它打印星號的正確數量,並以正確的順序:

* 
** 
*** 
** 
* 

感謝大家的幫助!

+0

Psssst,答案有方向,但如果您遇到問題,或只是好奇,我把一個解決方案在http://ideone.com/StJRS :) – 2012-07-26 04:35:32

+0

@RayToal,感謝幫助,但我不確定他是否希望我們使用Character包裝類或導入任何特定的包。 – ohayon 2012-07-26 14:53:30

+0

確實,那只是爲了好玩 - *眨眼*很高興你注意到!當然,使用循環 - 肯定比我在那裏扔的單行恐怖更快,更容易理解。 :) – 2012-07-26 15:38:25

回答

3

你需要另一個參數來告訴你你要走哪條路。您還需要測試結束條件,以瞭解何時從上升到下降。

public void patternMaker(int x, int direction){ 
    // Direction is either +1 or -1, depending on whether you go up or down 
    // at the moment. Once you reach 3, switch the sign; 
    // Once you reach zero, stop. 

    // Pseudocode: 
    // If x is zero, we're done 
    // Print x asterisks followed by newline 
    // if x == 3, make direction -1 
    // Perform the recursive call with (x+direction, direction) 
} 

在遞歸之前繪製恆星可能會更容易,儘管這兩種方法都是可行的。

+0

有意思,謝謝回覆!我會給這個鏡頭和霍勒回來。 – ohayon 2012-07-26 04:19:49

+0

所以我很喜歡這種方法,雖然我似乎無法完全實現它。我現在通過在遞歸調用之前和之後使用循環來雙向打印(仍然有一個參數),但是它的打印方式與我想要的方式相反。我不太確定如何使用stackoverflow向您展示我的當前代碼並再次打印出來,而不會發布其他問題,因此我將在此處發佈鏈接。 – ohayon 2012-07-26 14:45:58

+0

我剛纔發現我應該編輯我原來的問題,所以修改如上。非常感謝! – ohayon 2012-07-26 14:52:04

4

我不知道你可以遞歸一個參數。我認爲你需要兩個。這裏的想法:

stars(2, 4) 

將打印

** 
*** 
**** 
*** 
** 

stars(5, 6) 

將打印

***** 
****** 
***** 

這自然是遞歸的。爲什麼?因爲

stars(n, n) 

只是打印n顆星。這是基本情況。現在遞歸步驟怎麼樣?那麼讓我們試試吧

stars(k, n) 

其中k < n。這是這樣做的

draw a line of k stars 
call stars(k + 1, n) 
draw a line of k stars 

就是這樣。當然,有一個支票是好的,但我們相信你可以弄清楚。

+0

感謝您的迴應,我相信遞歸可以只用一個參數完成,但我不確定是否知道如何在最後實現該僞代碼。 – ohayon 2012-07-26 04:38:31

+0

如果你有一個參數,讓我們都知道。如果您最終需要關於僞代碼的幫助,請參閱我的完整工作Java解決方案,網址爲http://ideone.com/StJRS – 2012-07-26 04:55:44

1
static String patternMaker(int x, String result){ 
    String curStr =""; 
    for (int i=0; i<x; i++) 
     curStr += "*"; 
    curStr += "\n"; 
    if (result == null){    
     return patternMaker(x-1,curStr); 
    }else if (x > 0){   
      return patternMaker(x-1, curStr+result+curStr); 
    } 
    else 
     return result; 
} 

//test with : 
System.out.println(patternMaker(3,null)); 
+0

這是我從一開始就希望採用的路線。現在測試。謝謝! – ohayon 2012-07-26 04:43:07

-1
public class pattern { 

public void patternMaker(int x){ 
    if(x > 0){ 
     for(int i = 0; i < x; i++){ 
      for(int j=0;j<=i;j++){ 
       System.out.print("*"); 
      } 
      System.out.print("\n"); 
     } 
     for(int i = x-1; i > 0; i--){ 
      for(int j=i;j>0;j--){ 
       System.out.print("*"); 
      } 
      System.out.print("\n"); 
     } 
    } 
} 
public static void main(String[] ar){ 
    new pattern().patternMaker(3); 
} 
} 
+1

問題是關於遞歸......看起來你錯過了那部分。 – SiB 2012-07-26 08:02:43