There are many possible solutions to this problem. In general, there are many good strategies you would typically use to solve this:
- Look for a pattern. Can you find a pattern that can be easily expressed through logic and algebraic operations?
- Can the problem be broken down into smaller, more manageable parts? Sometimes what appears to be a complex problem can be separated into smaller, very simple problems.
- When in doubt, work things out on paper.
Let's take your example. At a glance, personally, I would divide the output into two halves; the top, consisting of a single triangle, and the bottom, consisting of a two triangles side by side. Further, let's focus on just being able to draw a triangle at all. Let's say we want to produce this, a triangle at an arbitrary location:
11
012345678901
0 x
1 xxx
2 xxxxx
We notice the triangle is centered on column 7, and we suspect it will help us if we can draw a triangle in any column, so let's let center
be the center column of our triangle. A lot of these types of strategies involve coming up with a way to parameterize the problem. For this approach we want to find the answer to this question:
- Given
row
, column
, and center
, should we draw a character at that location?
Let's take a simple algebraic approach first, one row at a time, and see if we notice any patterns:
row == 0
: Here we output only when column == center
.
row == 1
: Here we output when column >= center - 1 && column <= center + 1
.
row == 2
: Here we output when column >= center - 2 && column <= center + 2
.
Notice a pattern? Think about it for a second. Recognizing that row == 0
isn't actually a special case, the pattern is:
- Output when
column >= center - row && column <= center + row
.
Great! Now we can output a triangle very easily:
int center = 7; // From our example.
for (int row = 0; row < 3; ++ row) {
for (int column = 0; column < 11; ++ column) {
if (column >= center - row && column <= center + row)
System.out.print("x"); // Replace with whatever character to print.
else
System.out.print(" ");
}
System.out.println(); // Line break after each row, of course.
}
But what about two triangles, for the bottom half? The simplest of course would be to do the exact same as above, but since we have two triangles, we have two centers (say, centerL
and centerR
), and can simply add a second if
block in our bottom-half loop -- same logic for both centers, all in one loop. I'll leave this as an exercise to you.
Now, like I said, there are many possible solutions. Choose the one that makes the most sense for you and is easiest for you to get your head around. In fact, as a learning exercise, I would suggest trying to implement this program with at least three different algorithms. For example:
- Loop over all 6 rows (instead of top and bottom half) and put all 3 triangles in the same loop.
- Create a 2D array and draw the triangles into it, then output the contents.
- Try to implement the above without using
if
at all (e.g. loops to output from center - row
to center + row
, and separate loops for the borders) - this is similar to your current approach.
- Try to create a method that can draw triangles of any height, not just 3.
- Try to create a method that can draw any "triangle of triangles", e.g. 3, 4, 5 rows of triangles.
And of course,
