Java – draw a Christmas tree with “X”

I'm trying some online exercises in Java I have done a lot of exercises, but if n = 4, I will stick to an exercise that gives a given n (where n is the input from the user) and draw a Christmas tree as follows:

X
   X
  XXX
   X
  XXX
 XXXXX
   X
  XXX
 XXXXX
XXXXXXX

I can't seem to get around the loop This is what I have done 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);
    }
}

Solution

It is very good to write a triangle method and then call it in the 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 must consider is the number of spaces used This is the maximum parameter because only by making triangles of some sizes inconsistent with the space of other triangles

Therefore, no matter how large a triangle you build, you must always consider the largest triangle (the last) and how much space you need

The method of making only pure triangles is as follows: (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 with tree 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
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>