JAVA Print Triangle
Category Programming Techniques
First, determine what our output result is:
So how can we achieve this?
- First, analyze the structure of the shape
We can see that the shape has 5 rows. Can we create a for loop statement to control it within 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.
- Next, analyze how the shape is constructed. We can break down the shape into the following parts: /p
We can break down the shape into these three triangles.
- Establish the first empty triangle
It can be seen that the first line outputs 4 spaces, the second line outputs 3 spaces, the third line outputs 2 spaces, the fourth line outputs 1 space, and the fifth line has none.
From this pattern, we can see that it is a pattern of gradual decrease. So how can we 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. So how to implement it? See the code:
for(int i = 1;i<=5 ;i++) {
for(int j = 5; j >= i ; j--)//Establish the first shape
System.out.print(" ");
System.out.println();
}
The first for statement is the five-time loop statement we just defined.
The second for loop, let's analyze it:
First, define an int type variable j and assign the value of 5 to j
Then we think, since we want to shorten the distance, each time the loop j -1, it just meets our requirements:
The first big loop i=1, j=5, so it meets the condition j>=i and then outputs a space, then j-1, now the value of j is 4, which meets j>=i, and then outputs
...
Until j=0, j>=i does not meet, exit the inner loop
Now to System.out.println(); line break
Now back to the outer loop i++, i becomes 2, meets i<=5, enter the inner loop
Define j=5, j>=i, meet, output a space, j-1
j is now 4, j>=i, meet, output a space, j-1
...
Until j=1, j>=i does not hold, exit the inner loop, then line break
Then i+1 and then enter the inner loop again...
So the loop goes on to form a four-line inverted triangle, the first pattern is formed.
- Establish the second shape, and the principle is exactly the same as the first shape, but exactly the opposite
for(int i = 1 ;i<=5 ;i++){ for(int j = 5; j >= i ; j--)//Establish the first shape System.out.print(" "); for(int j = 1; j <= i; j++)//Establish the second shape System.out.print("*"); System.out.println(); }
As with the establishment of the first shape, everyone can understand it by themselves, so the second one is established.
- Establish the third shape
for(int i = 1; i <= 5; i++){ for(int j = 5 ;i <= j; j--)//Establish the first shape System.out.print(" "); for(int j = 1; j <= i; j++)//Establish the second shape System.out.print("*"); for(int j = 1; j < i; j ++)//Establish the third shape System.out.print("*"); }
Similarly, like the first and second, the principle of establishing the third shape is the same.
But everyone should pay attention to one point, the third shape did not output in the first line, so it should be cut off in the first big loop, let it output in the second big loop
So this time the condition is j < i, the equal sign is 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