Reputation: 1829
I have to write a program that takes a size value and turns it into a right triangle (made up of #
characters) such that the output looks like:
#
#
###
#####
#
###
#####
#######
The first triangle is of size 1.
The second triangle is of size 4.
The third triangle is of size 6.
Notice that the the number of characters per each line in a given triangle is always odd.
Also notice that the height of each triangle at the largest point can be calculated by size/2 + 1
I have attempted this problem, but I am stuck figuring out how I can display the correct number of #
symbols per line.
Here is my own code:
public class triangle {
public static void drawTriangle(int size) {
int column = (size/2) + 1;
for (int i = 0; i <= column; i++) {
for (int j = size; j >= 0; j--) {
if (i <= j) {
System.out.print(" ");
}
else {
System.out.print("#");
}
}
System.out.println();
}
}
public static void main(String[] args) {
drawTriangle(1);
drawTriangle(4);
drawTriangle(6);
}
}
And here is the output from my code:
#
#
##
###
#
##
###
####
As you can see I have arranged the triangles in the proper way, and have gotten the height of each triangle to be the way it supposed to. I just have no idea how I can get the correct number off characters on each line...
I have tried several things including changing my first for loop from:
for (int i = 0; i <= column; i++)
to
for (int i = 1; i <= column; i+=2)
Which filters out the even numbered rows, but fails to address the other parameters such as height of each triangle, and additional rows. Here is the output for my changes:
#
#
###
#
###
Any help is appreciated, thanks.
Upvotes: 2
Views: 1284
Reputation: 353
Here you go:
public static void drawTriangle(int size) {
for (int i = 1; i <= size + 1; i += 2) {
for (int spaces = 0; spaces <= size - i; spaces++) {
System.out.print(" ");
}
for (int hashes = 0; hashes < i; hashes++) {
System.out.print("#");
}
System.out.println();
}
System.out.println();
}
As Code-Apprentice said, it makes sense for there to be one main for
loop to display each row in a triangle. Then, in order to display the correct number of spaces and hashes in each row, I used two nested for
loops.
Update: Simplified and improved code (using Code-Apprentice's suggestion)
Upvotes: 2
Reputation: 9855
Used a simple StringUtil, but you could write it without that also.
public static void drawTriangle(int size) {
int maxRow = size + 1;
for (int i = 1; i <= maxRow; i+=2) {
System.out.println(StringUtils.repeat(" ", maxRow-i) + StringUtils.repeat("#", i));
}
}
Upvotes: 2
Reputation: 66989
For a given size
, every row contains size + 1
characters (either " " or "#"). The number of leading spaces in the first row is size
, and the number decrements by 2 with every row.
This simple method implements the above rules:
public static void drawTriangle(int size) {
int nSpaces = size;
for (int i = (size/2) + 1; --i >= 0; ) {
for (int j = 0; j <= size; j++) {
System.out.print(j < nSpaces ? ' ' : '#');
}
System.out.println();
nSpaces -= 2;
}
}
Upvotes: -1
Reputation: 83527
Let's take a step back from the code and think about this in English. A first attempt to do this makes me think of the following:
For each row
output the correct number of spaces and #s
The first line shows that the outer for loop needs to count the number of rows. I think this is stored in your size
variable. I conclude from this that that your for loops are reversed. The outer loop should iterate over the rows and the inner loop should iterate over the columns.
Upvotes: 3