如何使用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
******
** ****
* ****
* ****
** ****
* ****
* ****
提示:注意在你给的例子任何模式? – Amber 2011-02-26 23:36:57
是的,它是功课。它已经过期了。我试图完成它,但不能。我正在寻找一种方法来做到这一点,所以我可以理解。 – 2011-02-26 23:40:52
到目前为止你有什么? – 2011-02-26 23:44:18