50 lines
1.4 KiB
Java
50 lines
1.4 KiB
Java
import java.util.Scanner;
|
|
|
|
// Let's build a Christmas tree.
|
|
// Reference: https://www.javatpoint.com/christmas-tree-pattern-in-java
|
|
public class ChristmasTree {
|
|
public static void main(String[] args) {
|
|
Scanner sc = new Scanner(System.in);
|
|
|
|
// tree height (number of stories)
|
|
System.out.print("Enter height: ");
|
|
int height = sc.nextInt();
|
|
|
|
// tree width (width of the smallest story)
|
|
System.out.print("Enter width: ");
|
|
int width = sc.nextInt();
|
|
|
|
int padding = width * 5;
|
|
|
|
int x = 1;
|
|
for (int a = 1; a <= height; a++) {
|
|
for (int i = x; i <= width; i++) {
|
|
for (int j = padding; j >= i; j--) {
|
|
// print padding
|
|
System.out.print(" ");
|
|
}
|
|
for (int k = 1; k <= i; k++) {
|
|
// print actual tree
|
|
System.out.print("* ");
|
|
}
|
|
System.out.println();
|
|
}
|
|
x = x + 2;
|
|
width = width + 2;
|
|
}
|
|
|
|
// print stem
|
|
for (int i = 1; i <= 4; i++) {
|
|
for(int j = padding-3; j >= 1; j--) {
|
|
System.out.print(" ");
|
|
}
|
|
|
|
for(int k= 1; k <= 4; k++) {
|
|
System.out.print("* ");
|
|
}
|
|
|
|
System.out.println();
|
|
}
|
|
}
|
|
}
|