1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package xmas;

public class Tree {

public static void main(String[] args) {
// TODO 自動產生的方法 Stub


//=======================================================
int a = 8; // 幾大層
int b = 3; // 第一大層有幾小層
int c = 1; // 每一大層增加幾小層
int d = 1; // 每小層加幾片葉子
int aa = 5; // 樹幹長度
int bb = 7; // 樹幹寬度(建議單數)
//=======================================================

int e = b + (a - 1) * c; // 最後一層有幾小層
int f = 1 + (e - 1) * (2 * d); // 最後一層會有幾片葉子
for (int x = 0; x < a; x++) {
int g = b + x * c; // 這一大層有幾小層
int h = 1 + (g - 1) * d * 2; // 這一大層的最後一小層有幾片葉子
for (int y = 0; y < g; y++) {
int i = 1 + y * d * 2; // 這一層需要幾片葉子
for (int z = 0; z < (f - h) / 2; z++) {
System.out.print(" ");
}
for (int z = 0; z < (h - i) / 2; z++) {
System.out.print(" ");
}
for (int z = 0; z < i; z++) {
System.out.print("*");
}
System.out.println("");
}
}
for (int x = 0; x < aa; x++) {
for (int y = 0; y < (f - bb) / 2; y++) {
System.out.print(" ");
}
for (int y = 0; y < bb; y++) {
System.out.print("*");
}
System.out.println("");
}
}
}