2011-02-26 68 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(); 
    } 
}