Demo08.java 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. /**
  2. * @author WanJl
  3. * @version 1.0
  4. * @title Demo08
  5. * @description 课堂练习:使用嵌套 for 循环打印一个 5 行的菱形星号图案(上下对称,第 3 行最多)
  6. * @create 2026/7/15
  7. */
  8. public class Demo08 {
  9. public static void main(String[] args) {
  10. for (int i = 1; i <=5 ; i++) {
  11. for (int j = 1; j <=5; j++) {
  12. if(i==3){ //不输出任何空格的
  13. System.out.print("*");
  14. }else{ //有空格的 1、2、4、5行
  15. if (i==1||i==5){ //第1行 第5行
  16. if (j==3) {
  17. System.out.print("*");
  18. }else {
  19. System.out.print(" ");
  20. }
  21. }else if(i==2||i==4){
  22. if (j==1||j==5){
  23. System.out.print(" ");
  24. }else {
  25. System.out.print("*");
  26. }
  27. }
  28. }
  29. }
  30. System.out.println();
  31. }
  32. }
  33. }