1.实心菱形
public class Demo9 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
//空格
for (int j = 1; j <= 5 - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2*(5-i)-1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
2.空心菱形
public class Demo10 {
public static void main(String[] args) {
//行数
for (int i=1;i<=5;i++) {
//空格
for(int j=1;j<=5-i;j++) {
System.out.print(" ");
}
//列数
for(int j=1;j<=2*i-1;j++) {
if(j==1 || j==2*i-1){
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.println();
}
//倒三角形的行数
for (int i=1;i<=4;i++) {
for (int j=1;j<=i;j++) {
System.out.print(" ");
}
//列数
for (int j=1;j<=2*(5-i)-1;j++) {
if(j==1 || j==2*(5-i)-1) {
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.println();
}
}
}