密实的菱形
效果
*
***
*****
*******
*********
*******
*****
***
*
代码
public static void main(String[] args) {
int max = 6;
for (int i = 0; i < max; i++){
for (int j = max - 1; j > i; j--){
System.out.print(" ");
}
for (int j = 0; j < 2 * i - 1; j++){
System.out.print("*");
}
System.out.print("\n");
}
for (int i = max - 2; i > 0; i--){
for (int j = i; j < max - 1; j++){
System.out.print(" ");
}
for (int j = 2 * i - 1; j > 0; j--){
System.out.print("*");
}
System.out.print("\n");
}
}
间隔的菱形
效果
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
代码
public static void main(String[] args) {
int max = 6;
for (int i = 0; i < max; i++){
for (int j = max - 1; j > i; j--){
System.out.print(" ");
}
for (int j = 0; j < 2 * i - 1; j++){
if ((j + 1) % 2 != 0){
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.print("\n");
}
for (int i = max - 2; i > 0; i--){
for (int j = i; j < max - 1; j++){
System.out.print(" ");
}
for (int j = 2 * i - 1; j > 0; j--){
if (j % 2 != 0){
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.print("\n");
}
}
网友评论