Skip to content

Instantly share code, notes, and snippets.

@HabaCo
Created May 29, 2014 22:42
Show Gist options
  • Save HabaCo/707312ec3d6485f75643 to your computer and use it in GitHub Desktop.
Save HabaCo/707312ec3d6485f75643 to your computer and use it in GitHub Desktop.
Print星星
public class Test03 {
public static void main(String[] args) {
int length = 13; // 行數可任意變更,配合 work4 建議奇數為主
System.out.println("Work1..");
for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++)
System.out.print("★");
System.out.println();
}
System.out.println("Work2..");
for (int i = 0; i < length; i++) {
for (int j = 0; j <= i; j++)
System.out.print("★");
System.out.println();
}
System.out.println("Work3..");
for (int i = 0; i < length; i++) {
for (int j = length; j > i; j--)
System.out.print("★");
System.out.println();
}
System.out.println("Work4.."); // 令 n = 行數, L = 長
for (int i = 0; i < length / 2 + 1; i++) {
for (int j = length / 2; j > i; j--)
// 白色星星隨著行數(n)遞減為(L/2-n)個
System.out.print("☆");
for (int j = 0; j < 2 * (i + 1) - 1; j++)
// 黑色星星隨著行數(n)遞增為(2*n-1)
System.out.print("★");
for (int j = length / 2; j > i; j--)
// 白色星星隨著行數(n)遞減為(L/2-n)個
System.out.print("☆");
System.out.println();
}
System.out.println("Work5..continue Work4"); // 接續 Work4 使之成為菱形
for (int i = length / 2 + 1; i < length; i++) {
for (int j = length / 2; j < i; j++)
// 白色星星隨著行數(n)遞增為(n-L/2)個
System.out.print("☆");
for (int j = 0; j < 2 * (length - i) - 1; j++)
// 黑色星星隨著行數(n)遞減為(2*(L-n)-1)
System.out.print("★");
for (int j = length / 2; j < i; j++)
// 白色星星隨著行數(n)遞增為(n-L/2)個
System.out.print("☆");
System.out.println();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment