闲来无事研究了下如何打印星树.
不知还有什么更好的方法
代码如下:
public static void main(String[] args) {
int spaceCount = 0;
int starCount = 0;
for (int i = 0; i < LINES; i++) {
starCount = i * 2 + 1;
if (starCount > LINES) {
starCount = LINES * 2 - starCount;
}
spaceCount = (LINES - starCount)/2;
for (int j = 0; j < spaceCount; j++) {
System.out.print(" ");
}
for (int k = 0; k < starCount; k++) {
System.out.print("*");
}
System.out.println();
}
}
打印结果
*
***
*****
*******
*****
***
*
***
*****
*******
*****
***
*