如何使用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