当前位置: 代码迷 >> 综合 >> for打印直角三角形,等腰三角形,等边三角形,正菱形
  详细解决方案

for打印直角三角形,等腰三角形,等边三角形,正菱形

热度:70   发布时间:2024-01-03 12:37:57.0

public class Chart0505 {
//for打印三角形

public static void main(String[] args) {//直角三角形for (int i = 1; i <= 4; i++) {for (int j = 1; j <= i; j++) {System.out.print("*");}System.out.println();}//等腰三角形
for(int i = 1;i <= 4;i++) {for (int j = 1; j <= (4-i); j++) {System.out.print(" ");}for (int k = 1; k <= (2 * i - 1); k++) {System.out.print("*");}System.out.println();
}
//等边三角形
for (int i = 1; i <= 4; i++) {for (int j = 1; j <= (4-i); j++) {System.out.print(" ");}for (int k = 1; k <= i; k++) {System.out.print("* ");}System.out.println();
}
//正菱形
for (int i = 1; i <= 4; i++) {for (int j = 1; j <= (4-i); j++) {System.out.print(" ");}for (int k = 1; k <= i; k++) {System.out.print("* ");}System.out.println();
}
for (int i = 1; i <= 3; i++) {for (int j = 1; j <= i; j++) {System.out.print(" ");}for (int k = 1; k <= (4-i); k++) {System.out.print("* ");}System.out.println();
}

}
}