draw a christmas tree with "X"

10,514

It is good to write method for making triangles and then call it in method when creating christmas tree :

public static void main(String[] args) {
    drawChristmasTree(4);
}

 private static void drawChristmasTree(int n) {
     for (int i = 0; i < n; i++) {
         triangle(i+1,n);
     }
}

 private static void triangle(int n, int max){
     for (int i = 0; i < n; i++) {
         for (int j = 0; j < max-i-1; j++) {
             System.out.print(" ");
         }
         for (int j = 0; j < i*2+1; j++) {
             System.out.print("X");
         }
         System.out.println("");
     }
 }

The only difference you have to think about and which is unusual is the number of spaces used. This is that max parameter, because only by making triangles of some size dont fit up with the spaces to other triangles.

So no matter how big triangle you are building, you have to always think about the max triangle (the last one) and how much spaces you need.

The method only for making pure triangles looks like this : (there is only the difference of changing max-i-1 to n-i-1 )

 private static void triangle(int n){
     for (int i = 0; i < n; i++) {
         for (int j = 0; j < n-i-1; j++) {
             System.out.print(" ");
         }
         for (int j = 0; j < i*2+1; j++) {
             System.out.print("X");
         }
         System.out.println("");
     }
 }

Output for tree of size 10 :

         X
         X
        XXX
         X
        XXX
       XXXXX
         X
        XXX
       XXXXX
      XXXXXXX
         X
        XXX
       XXXXX
      XXXXXXX
     XXXXXXXXX
         X
        XXX
       XXXXX
      XXXXXXX
     XXXXXXXXX
    XXXXXXXXXXX
         X
        XXX
       XXXXX
      XXXXXXX
     XXXXXXXXX
    XXXXXXXXXXX
   XXXXXXXXXXXXX
         X
        XXX
       XXXXX
      XXXXXXX
     XXXXXXXXX
    XXXXXXXXXXX
   XXXXXXXXXXXXX
  XXXXXXXXXXXXXXX
         X
        XXX
       XXXXX
      XXXXXXX
     XXXXXXXXX
    XXXXXXXXXXX
   XXXXXXXXXXXXX
  XXXXXXXXXXXXXXX
 XXXXXXXXXXXXXXXXX
         X
        XXX
       XXXXX
      XXXXXXX
     XXXXXXXXX
    XXXXXXXXXXX
   XXXXXXXXXXXXX
  XXXXXXXXXXXXXXX
 XXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXX
Share:
10,514

Related videos on Youtube

emi
Author by

emi

I write about Ruby and related subjects on my blog.

Updated on September 15, 2022

Comments

  • emi
    emi over 1 year

    I'm trying some online excercises in Java. I did many of the exercises but I'm stuck at this one that says for a given n (where n is input from user) if n = 4, draw a Christmas tree that looks like this:

       X
       X
      XXX
       X
      XXX
     XXXXX
       X
      XXX
     XXXXX
    XXXXXXX
    

    I can't seem to get my head around the looping. This is what I did so far:

    public class Test {
    
        public double org, mes;
    
        public Test() {
        }
    
        private static void drawChristmasTree(int n) {
            if (n == 1) {
                System.out.println("X");
            } else {
                for (int p = 1; p <= n; p++) {
                    for (int i = 1; i <= n; i++) {
    
                        for (int j = 0; j < n - i; j++) {
                            System.out.print(" ");
                        }
                        for (int j = 0; j < (2 * i - 1); j++) {
                            //System.out.println("X");
                            System.out.print("X");
                        }
                        System.out.println();
                    }
                }
            }
        }
    
        public static void main(String[] args) {
            drawChristmasTree(4);
        }
    }
    
    • rgettman
      rgettman over 10 years
      Be careful of the "lopping" around your head. (I think you meant "looping".)
    • user2864740
      user2864740 over 10 years
      Note that this can be viewed as drawing 4 triangles, one on top of each-other. The base of each triangle is the first line with 1, 3, 5 and 7 X's, respectively. This repetitive nature can be used to derive a formula. Or, perhaps cheating, to call a "draw triangle" method the appropriate number of times with the appropriate base.
  • emi
    emi over 10 years
    I really appreciate your time and effort, libik :)