学完for循环,打印一个空心菱形练练手。但感觉这个代码有些麻烦,谁有更优化 代码,请大神指教。
代码如下:
class TestLingXingKong {
public static void main(String[] args) {
for (int j = 0;j <=6 ;j++ ) {
System.out.print(" ");
}
System.out.println("*");
for (int i = 0; i < 6 ;i++ ) {
for (int j = 0;j < 6-i ;j++ ) {
System.out.print(" ");
}
System.out.print("*"+" ");
for (int j = 0;j <= i-1 ;j++ ) {
System.out.print(" ");
}
System.out.print("*"+" ");
System.out.println();
}
for (int i = 6; i >= 0 ;i-- ) {
for (int j = 0;j < 6-i ;j++ ) {
System.out.print(" ");
}
System.out.print("*"+" ");
for (int j = 0;j <= i-1 ;j++ ) {
System.out.print(" ");
}
System.out.print("*"+" ");
System.out.println();
}
for (int j = 0;j <=6 ;j++ ) {
System.out.print(" ");
}
System.out.println("*");
}
}
|