This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi, I'm trying to output figures of Christmas trees using a method that accepts two parameters, segment and height. I just can't get the spacing right!!! They should be centered, and I can only use very basic level Java. Thank you so much for any help!!
public class ChristmasTrees
public static void main(String args)
public static void drawTree(int segments, int height)
for (int i = 1; i <= segments; i++)
for (int line = 1; line <= height; line++)
for (int spaces = 0; spaces <= (height - line); spaces++)
I think you have one too many loops. The outermost loop is once per "segment" (I'm not sure what that means), and the one just inside that is for "lines". The loop for lines starts off giving the line with 1 star, then the line with 3, and so forth through the 4 lines of the tree, and works correctly as near as I can tell. But then your "segment" loop starts it all over again with the line that has 3 stars and does 4 lines, then starts over with 5 stars, etc. I *think* what you meant was only the loop with the lines and the one for spaces and stars; I don't think you need the outermost loop.
Now, if your problem is that the stars on the different lines don't line up, then likely you are outputting this (or looking at the output) in some place that does "proportional spacing"; this means that each character only takes up as much space as its font thinks it needs, and stars and spaces won't take up the same amount of space. If you put the output into a file, and then into, say, Windows Notepad, they ought to line up because Notepad's default font is a "fixed-width" font, i.e., has the same horizontal space taken up by each character.
Joined: Sep 30, 2011
For the first one (3, 4) it should come out like this: (I don't think my algorithm for spacing in the method is right)
Note: this should be center aligned haha, it comes out left aligned.
For the first one (3, 4) it should come out like this:
I'm probably just being thick here, but I still don't understand exactly what 'segment' and 'height' are.
Am I right in thinking that the 'segments' are the triangles that get progressively larger, and 'height' is the number of lines used to draw a segment?
If so, you might want to break down the process for drawing a tree into its component parts (something you should get into the practise of doing for ALL programs). Maybe something like, in my bad pseudo-code:I'll leave the rest to you, but it may help you see things a bit more clearly, rather than trying to handle all those nested loops.
Joined: May 29, 2005
Please look at the second paragraph of my response above. It addresses the spacing problem.
I don't see a way to upload an image from my machine to javaranch or I'd show you, but when I run this the output looks center-aligned with a fixed-width font.
Both your responders so far have asked what a segment is; knowing that is essential to understanding what you are trying to do, though it may not be related to the spacing problem.