# 迴圈列印的把戲

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
评论
01-06
03-27
11-11
02-22

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

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

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