First, determine our output result:

So how can we achieve this?

1. First, analyze the structure of the figure

We can see that the figure has 5 rows in total. 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, as the outermost loop.

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

We can split the figure into these three triangles.

3. Create blank triangle No. 1

It can be seen that 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 it is a decreasing pattern. So how to implement it?

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

Can we use this principle? The answer is of course. Then how to implement it? Please 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-time loop statement just defined.

The second for loop, let us analyze it:

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

Then we think, since we want to shorten the distance, each loop j is decremented by 1, 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's value is 4, meets j>=i, output again.

……

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

Now go to System.out.println(); to change line.

Now return to the outer loop i++, i becomes 2, meets i<=5, enter the inner loop.

Define j=5, j>=i, meets, output a space, j-1.

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

……

Until j=1, j>=i is not true, break out of the inner loop, then change line.

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

By looping like this, an inverted triangle of four rows is formed, and pattern No. 1 is completed.

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();
}

Same as creating pattern No. 1, everyone can understand it by themselves. Thus 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 as pattern No. 1 and No. 2 are the same, the principle for creating pattern No. 3 is the same.

But pay attention to one point: pattern No. 3 is not output in the first row, so it should be cut off in the first outer loop, and let it be output in the second outer loop.

Therefore, the judgment condition this time is j < i, with the equals sign 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(); } } }

Contributing author's email: [email protected]