思路是建立两个等边三角形,上面的循环5次下面的循环4次,合并成一个菱形。
public static void main(String[] args) {
for(int i = 1; i <=4 ; i++){
for(int j = 1; j <= 4; j++){
if(i + j < 5 ){
System.out.print(" ");
}else{
System.out.print(" "+"*");
}
}
System.out.println();
}
for(int i = 5; i >=1 ; i--){
for(int j = 1; j <= 5; j++){
if(i + j < 6 ){
System.out.print(" ");
}else{
System.out.print("*"+" ");
}
}
System.out.println();
}
}
运行截图