| 123456789101112131415161718192021222324252627282930313233 |
- /**
- * @author WanJl
- * @version 1.0
- * @title Demo08
- * @description 课堂练习:使用嵌套 for 循环打印一个 5 行的菱形星号图案(上下对称,第 3 行最多)
- * @create 2026/7/15
- */
- public class Demo08 {
- public static void main(String[] args) {
- for (int i = 1; i <=5 ; i++) {
- for (int j = 1; j <=5; j++) {
- if(i==3){ //不输出任何空格的
- System.out.print("*");
- }else{ //有空格的 1、2、4、5行
- if (i==1||i==5){ //第1行 第5行
- if (j==3) {
- System.out.print("*");
- }else {
- System.out.print(" ");
- }
- }else if(i==2||i==4){
- if (j==1||j==5){
- System.out.print(" ");
- }else {
- System.out.print("*");
- }
- }
- }
- }
- System.out.println();
- }
- }
- }
|