First, determine our output result:

So how can we achieve this?

1. First, analyze the structure of the graphic

We can see that the graphic has 5 rows. So, can we create a for loop statement to control it to 5 rows? The answer is yes.

for(int i = 1 ;i <= 5 ;i++ ){

}

In this way, we have created a for loop code block that loops 5 times, which is the outermost loop.

2. Then, analyze how the graphic is composed. We can split the graphic into the following parts: /p>

We can split the graphic into these three triangles.

3. Create triangle No. 1, the blank triangle

As you can see, the first line outputs 4 spaces, the second line outputs 3 spaces, the third line outputs 2, the fourth line outputs 1, and the fifth line has none.

From this pattern, we can see that it decreases successively. So how do we implement it?

We can imagine that from 1 to 5, there are four numbers in between; from 2 to 5, there are 3 numbers; from 3 to 5...

Can we use this principle? Of course, the answer is yes. So how to implement it? Look at the code:

for(int i = 1;i<=5 ;i++) {
    for(int j = 5; j >= i ; j--)//建立1号图形
        System.out.print(" ");
    System.out.println();
}

The first for statement is the five-iteration loop statement just defined.

Now let's analyze the second for loop:

First, define an int variable j and assign it a value of 5.

Then we think, since we want to shorten the distance, j decrements by 1 each loop, which exactly meets our requirement:

In the first outer loop, i=1, j=5, so it meets the condition j>=i, then outputs a space, then j-1, now j is 4, meets j>=i, and outputs again.

……

Until j=0, j>=i is not met, and it breaks out of the inner loop.

Now it reaches System.out.println(); and moves to a new line.

Now back to the outer loop, i++, i becomes 2, meets i<=5, and enters the inner loop.

Set j=5, j>=i is met, output a space, j-1.

j is now 4, j>=i is met, output a space, j-1.

……

Until j=1, j>=i is not true, break out of the inner loop, then move to a new line.

Then i+1, and then enter the inner loop again...

And so on, this forms an inverted triangle with four rows, and pattern No. 1 is created.

4. Create pattern No. 2. The principle is exactly the same as pattern No. 1, but just the opposite.

for(int i = 1 ;i<=5 ;i++){
    for(int j = 5; j >= i ; j--)//建立1号图形
        System.out.print(" ");
    for(int j = 1; j <= i; j++)//建立2号图形
        System.out.print("*");
    System.out.println();
}

It is the same as creating pattern No. 1; you can understand it yourself. In this way, pattern No. 2 is created.

5. Create pattern No. 3

for(int i = 1; i <= 5; i++){
    for(int j = 5 ;i <= j; j--)//建立1号图形
        System.out.print(" ");
    for(int j = 1; j <= i; j++)//建立2号图形
        System.out.print("*");
    for(int j = 1; j < i; j ++)//建立3号图形
        System.out.print("*");

}

Similarly, just like patterns No. 1 and No. 2, the principle for creating pattern No. 3 is the same.

But note one thing: pattern No. 3 is not output in the first row, so we need to cut it off in the first outer loop and let it output in the second outer loop.

Therefore, this time the condition is j < i, with the equality removed.

Complete source code:

class Demo{ public static void main(String[] args){ for(int i=1;i<=5;i++){ for(int j=5; i<=j; j--) System.out.print(" "); for(int j=1; j<=i; j++) System.out.print("*"); for(int j=1; j<i; j++) System.out.print("*"); System.out.println(); } } }

Contributor's email: 1030691530@qq.com