Advertisement
KuoHsiangYu

印聖誕樹

Jul 5th, 2020
766
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. package com.sample;
  2.  
  3. import static java.lang.System.out;
  4.  
  5. public class ChristmasTree1 {
  6.  
  7.     public static void main(String[] args) {
  8.         printTriangle(4, 2);
  9.         printTriangle(4, 3);
  10.         printTriangle(4, 4);
  11.         printTrunk(4, 2);
  12.     }
  13.  
  14.     private static void printTrunk(int width, int height) {
  15.         width = width - 1;
  16.         for (int i = 1; i <= height; i++) {
  17.             for (int j = 1; j <= width; j++) {
  18.                 out.print(" ");
  19.             }
  20.             out.print("*");
  21.             out.println();
  22.         }
  23.     }
  24.  
  25.     private static void printTriangle(int width, int height) {
  26.         for (int i = 1; i <= height; i++) {
  27.             for (int j = 1; j <= (width - i); j++) {
  28.                 out.print(" ");
  29.             }
  30.             for (int j = 1; j <= (2 * i) - 1; j++) {
  31.                 out.print("*");
  32.             }
  33.             out.println();
  34.         }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement