迴圈列印的把戲

最近解了幾個迴圈列印的問題,想到以前寫的得意作品,就保存在這裡一份好了,想看結果就剪下貼上。
星星之一

public class Star {
    public static void main(String[] args) {
        //改變這個值,可以改變星星大小
        int max = 4;
        for (int i = 0; i < (4 * max - 3); i++) {
            if (i < (max - 1)) {
                printStar(max + i, i + 1, (3 * max - 3 - i) / 2);
            } else if (i < (2 * max - 1)) {
                printStar(max + i, 4 * max - 3 - i, (i - max + 1) / 2);
            } else if (i < (3 * max - 2)) {
                printStar(max + i, i + 1, (3 * max - 3 - i) / 2);
            } else {
                printStar(max + i, 4 * max - 3 - i, (i - max + 1) / 2);
            }
        }
    }
    private static void printStar(int level, int starCount, int spcCount) {
        if (level % 2 == 0) {
            System.out.print(" ");
        }
        for (int i = 0; i < spcCount; i++) {
            System.out.print("  ");
        }
        for (int i = 0; i < starCount; i++) {
            System.out.print("* ");
        }
        System.out.println();
    }
}
星星之二
public class StarII {
    public static void main(String[] args) {
        // 改變這個值,可以改變星星大小
        int max = 5;
        for (int i = 0; i < (4 * max - 3); i++) {
            if (i < (max - 1)) {
                printStarII(max + i, i + 1, (3 * max - 3 - i) / 2);
            } else if (i < (2 * max - 1)) {
                if (i == (max - 1)) {
                    printStarIII(max + i, 4 * max - 3 - i, (i - max + 1) / 2, max - 2);
                } else {
                    printStarII(max + i, 4 * max - 3 - i, (i - max + 1) / 2);
                }
            } else if (i < (3 * max - 2)) {
                if (i == (3 * max - 3)) {
                    printStarIII(max + i, i + 1, (3 * max - 3 - i) / 2, max - 2);
                } else {
                    printStarII(max + i, i + 1, (3 * max - 3 - i) / 2);
                }
            } else {
                printStarII(max + i, 4 * max - 3 - i, (i - max + 1) / 2);
            }
        }
    }
    private static void printStar(int level, int starCount, int spcCount) {
        if (level % 2 == 0) {
            System.out.print(" ");
        }
        for (int i = 0; i < spcCount; i++) {
            System.out.print("  ");
        }
        for (int i = 0; i < starCount; i++) {
            System.out.print("* ");
        }
        System.out.println();
    }
    private static void printStarII(int level, int starCount, int spcCount) {
        if (level % 2 == 0) {
            System.out.print(" ");
        }
        for (int i = 0; i < spcCount; i++) {
            System.out.print("  ");
        }
        for (int i = 0; i < starCount; i++) {
            if (i == 0 || i == (starCount - 1)) {
                System.out.print("* ");
            } else {
                System.out.print("  ");
            }
        }
        System.out.println();
    }
    private static void printStarIII(int level, int starCount, int spcCount,
            int midsp) {
        if (level % 2 == 0) {
            System.out.print(" ");
        }
        for (int i = 0; i < spcCount; i++) {
            System.out.print("  ");
        }
        if (midsp > 0) {
            for (int i = 0; i < (starCount - midsp) / 2; i++) {
                System.out.print("* ");
            }
            for (int i = 0; i < midsp; i++) {
                System.out.print("  ");
            }
            for (int i = 0; i < (starCount - midsp) / 2; i++) {
                System.out.print("* ");
            }
        } else {
            for (int i = 0; i < starCount; i++) {
                System.out.print("* ");
            }
        }
        System.out.println();
    }
}
星星之三
public class StarIII {
  public static void main(String[] args) {
    int max = 7;
    int x = 4 * max - 3;
    int y = 6 * max - 5;
    char[][] starSpc = new char[x][y];
    for (int i = 0; i < x; i++) {
      for (int j = 0; j < y; j++) {
        starSpc[i][j] = ' ';
      }
    }
    for (int i = 0; i  0) {
        starSpc[i][y / 2 - i] = '*';
        starSpc[i][y / 2 + i] = '*';
      } else if (y / 2 - i == 0) {
        for (int j = 0; j < y; j += 2) {
          starSpc[i][j] = '*';
        }
      }
    }
    for (int i = max - 1; i < x; i++) {
      if (i == max - 1) {
        for (int j = 0; j < y; j += 2) {
          starSpc[i][j] = '*';
        }
      } else {
        starSpc[i][i - max + 1] = '*';
        starSpc[i][7 * max - 7 - i] = '*';
      }
    }
    for (int i = 0; i < x; i++) {
      System.out.println(starSpc[i]);
    }
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值