• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with spacing

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
{
drawTree(3, 4);
System.out.println();
drawTree(2, 5);
}

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++)
{
System.out.print(" ");
}

for (int dots = 1; dots <= (2*line - 1) + 2*(i-1); dots++)
{
System.out.print("*");
}
System.out.println();
}
}
System.out.println(" * ");
System.out.println(" * ");
System.out.println(" ******* ");
}
}
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

rc
 
James.Seung Lee
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

*
***
*****
*******
***
*****
*******
*********
*****
*******
*********
***********
*
*
*******
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

Winston
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

rc
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here is the code using Scanner to obtain the number of segments and height from the user. copy and paste messed up my indenting but you get the idea





 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch , john paschal. You may be a little late with your reply. I shall try to enhance it with the code button; if I succeed you can see how much better it looks.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic