2011-02-26 107 views
4

如何使用Java以給定大小遞歸地打印鑽石?Java:遞歸地打印鑽石

的5所述的尺寸生產:

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

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

代碼我迄今爲止

public static void dia(int statSize, int size,int count) { 

     int statSizeLarge = (statSize*2)+1; 

     // Params: 
     // statSize == static size, never change this 
     // size == variable size, change this 
     // count == counter 

     if(size==0) { 
       System.out.println(); 
     } else { 

      // is the counter smaller then the size 
      // if yes, increment and keep printing 
      if(count<size){ 
       System.out.print("*"); 
      } 



      // is greater then size? 
      // if yes, move on, print 
      // a few more stars 
       if((count<=statSizeLarge)){ 
        if(count<statSize+1 && (count>size)){ 
         System.out.print(" "); 
        }else if (count>size+1){ 
         System.out.print("*"); 
        } else {} 
        dia(statSize,size,count+1); 
       } 



     // reset count, move to next element 
      if(count>=statSizeLarge) { 
       count = 0; 
       System.out.println(); 
       dia(statSize,size-1,count); 
      } 



     } // ends Else 

    } 

輸出:

Enter commands: 
diamond 3 
****** 
** **** 
* **** 




* **** 




** **** 
* **** 




* **** 
+0

提示:注意在你給的例子任何模式? – Amber 2011-02-26 23:36:57

+1

是的,它是功課。它已經過期了。我試圖完成它,但不能。我正在尋找一種方法來做到這一點,所以我可以理解。 – 2011-02-26 23:40:52

+0

到目前爲止你有什麼? – 2011-02-26 23:44:18

回答

6

要創建更大的鑽石,取較小的一個,並添加兩個額外的行和列。在下面的diagrom中,爲了清晰起見,我用空格替換了空格。在第二顆鑽石中,新添加的字符以粗體顯示。

 
       *****.***** <-- extra row 
****.****  ****...**** 
***...***  ***.....*** 
**.....**  **.......** 
*.......*  *.........* 
......... --> ........... 
*.......*  *.........* 
**.....**  **.......** 
***...***  ***.....*** 
****.****  ****...**** 
       *****.***** <-- extra row 
        ^^ 
        || 
        extra columns 

您的遞歸函數應打印第一行,然後打印一個較小的鑽石,中間有兩個額外的列,然後是最後一行。

僞代碼:

void diamond(stars, spaces) { 
    if (n == 0) { 
     print(' ' * spaces) 
    } else { 
     print('*' * stars, ' ' * spaces, '*' * stars) 
     diamond(stars - 1, spaces + 2) 
     print('*' * stars, ' ' * spaces, '*' * stars) 
    } 
} 

由於這是一個學習的過程,我不會給你完整的Java源代碼 - 你可以在自己寫就一展身手。在這裏,你可以看到它在Python在線運行,只是讓你可以看到,該算法的工作原理:

+0

嗯......現在他還得學習Python :-) – 2011-02-27 02:58:40

1

提示:尋找在輸出的模式。嘗試將該模式映射到遞歸調用,方法執行某些操作,調用自己,然後執行其他操作。

0

這裏是Java程序打印的明星鑽石:

class DiamondPattern 
{ 
    static public int ReadInteger() 
    { 
     try 
     { 
       String inpString = ""; 
       InputStreamReader input = new InputStreamReader(System.in); 
       BufferedReader reader = new BufferedReader(input); 
       String s = reader.readLine(); 
       return Integer.parseInt(s); 
     } 
     catch (Exception e) 
     { 
       e.printStackTrace(); 
     } 
     return -1; 
    } 

    public static void main(String[] args) 
    { 
     System.out.println("Program for displaying pattern of *."); 
     System.out.print("Enter the maximum number of *: "); 
     int n = ReadInteger(); 

     System.out.println("\nHere is the Diamond of Stars\n"); 

     for (int i = 1; i <= n; i++) 
     { 
       for (int j = 0; j < (n - i); j++) 
        System.out.print(" "); 
       for (int j = 1; j <= i; j++) 
        System.out.print("*"); 
       for (int k = 1; k < i; k++) 
        System.out.print("*"); 
       System.out.println(); 
     } 

     for (int i = n - 1; i >= 1; i--) 
     { 
       for (int j = 0; j < (n - i); j++) 
        System.out.print(" "); 
       for (int j = 1; j <= i; j++) 
        System.out.print("*"); 
       for (int k = 1; k < i; k++) 
        System.out.print("*"); 
       System.out.println(); 
     } 

     System.out.println(); 
    } 
}