JAVA Printing Triangles
Category Programming Techniques
First, let's determine our output:
So how can we achieve this?
- Analyze the structure of the pattern
We can see that the pattern has 5 rows. Can we create a for loop to control this? The answer is yes.
for(int i = 1; i <= 5; i++) {
}
This way, we have created a for loop that repeats 5 times, serving as the outermost loop.
- Analyze how the pattern is formed
We can break down the pattern into the following parts:
We can divide the pattern into three triangles.
- Create the first blank triangle
Notice that the first row outputs 4 spaces, the second row outputs 3 spaces, the third row outputs 2 spaces, the fourth row outputs 1 space, and the fifth row outputs none.
This shows a decreasing pattern. How can we implement this?
We can think of it as having four numbers between 1 and 5, three numbers between 2 and 5, and so on.
Can we use this principle? The answer is yes. Here's how:
for(int i = 1; i <= 5; i++) {
for(int j = 5; j >= i; j--) // Create the first triangle
System.out.print(" ");
System.out.println();
}
The first for statement is the previously defined 5-time loop.
The second for loop is explained as follows:
First, define an integer variable j
and assign it the value 5.
To shorten the distance, we decrement j
by 1 each loop, which meets our requirement:
During the first major loop, i=1
, j=5
, so j>=i
is true, and a space is printed. Then j
is decremented to 4, which still meets j>=i
, so another space is printed.
...
Until j=0
, j>=i
is false, and the inner loop is exited.
Now System.out.println();
is executed for a line break.
Back to the outer loop, i
is incremented to 2, which still meets i<=5
, and the inner loop starts again.
Define j=5
, j>=i
is true, a space is printed, and j
is decremented.
j
is now 4, j>=i
is true, another space is printed, and j
is decremented.
...
Until j=1
, j>=i
is false, the inner loop is exited, and a line break is executed.
Then i
is incremented, and the inner loop starts again...
This cycle continues, forming a four-line inverted triangle, the first pattern.
- Create the second triangle, which is exactly the same as the first but opposite
for(int i = 1; i <= 5; i++) { for(int j = 5; j >= i; j--) // Create the first triangle System.out.print(" "); for(int j = 1; j <= i; j++) // Create the second triangle System.out.print("*"); System.out.println(); }
As with the creation of the first triangle, you can understand this yourself. The second triangle is thus formed.
- Create the third triangle
for(int i = 1; i <= 5; i++) { for(int j = 5; i <= j; j--) // Create the first triangle System.out.print(" "); for(int j = 1; j <= i; j++) // Create the second triangle System.out.print("*"); for(int j = 1; j < i; j++) // Create the third triangle System.out.print("*"); }
Similarly, the creation of the third triangle is the same as the first and second.
But note that the third triangle does not output on the first line, so it needs to be cut off during the first major loop and output from the second major loop.
Hence, the condition j < i
is used, excluding the equality.
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 Email: [email protected]