• 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

For Loop Problem

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to do something specific with a for loop. This is for an assignment in a class I am in, I want to say that upfront so there is no confusion. I need to make a forloop which prints the following:

*
**
***
****
*****
******
*******
********
*********
**********

It then is rotated 90 degrees through the next instance of the loop until it has gone a full 360.

What I have now does the sequence above. I know I need a nested for loop, but I am not sure how to implement this. The code I currently have is below. Any help is greatly appreciated. Thanks, Dennis

Code follows:

public class Forloop
{
public static void main( String args[] )
{

for ( int line_no = 1; line_no <= 10; line_no++ )
{
String print = "";
for ( int stars = 1; stars <= line_no ; stars++ )

{

print += "*";
}

System.out.println(print);
}

}

}
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like the above program will print the triangle made of *'s as you have shown. How can you change this to print the next "rotated triangle"? If I were doing this, I would just make another set of nested for loops similar to what you already have.

Does this make any sense? Think about it for a little bit and see what you can come up with. Feel free to use this forum as a sounding board for your ideas.

Layne
 
Dennis Myhand
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I understand I need another set of forloops nested in the forloop I have, and I stated that in my post. I have tried turning the loops every way I can think of and I get nothing but errors about line_no, print, and stars already being defined in main. Take those out of the loop I added and I get even more errors. I am not seeing HOW to implement a nested loop. I tried googling for this information and I only seem to be redirected to a Pharmacy site in India. Do you know of a specific site where this information might be?
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Yes, I understand I need another set of forloops nested in the forloop I have, and I stated that in my post


Why do you need further nested loops? Assuming you have already successfully created one of the four triangles, you already have the code you need? Can you see how that could be done without nesting more loops?
 
Dennis Myhand
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try drawing what you want the output to be and then think how you could modify your for loop to produce that output.
Remember that for loops can decrement as well as increment.
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dennis Myhand:
No.



Well, try looking again, bearing in mind what Joanne Neal has suggested. You might find it clearer if you write out in pseudo-code what you are trying to do before trying to write it in Java.
 
Dennis Myhand
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry folks, but your hints are too subtle for me. Any changes I make to the code seem to cause it to do really wierd things or stop working altogether. Can someone please point me to a tutorial?
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are not going to get a tutorial for something as specific as you need. There are plently of tutorials which will include the basics of loops (Sun's Java Basics tutorial, for example), but I'm assuming from what you've already posted you now this anyway. Perhaps you could post what you are trying (and is giving you so much trouble) then some kind people here might point you in the right direction.
[ September 19, 2005: Message edited by: Paul Sturrock ]
 
Dennis Myhand
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I need to do:

Display the following:


What I have done, code wise:


As you can see from the commented out section, I have tried adding another loop, I have tried ++line_no, I have tried ++stars, I have tried line_no--, I have tried stars--, I have tried --line_no and --stars. I am at a loss as to what to try next.

//JAM -- Added [CODE] and [/CODE] tags.
[ September 19, 2005: Message edited by: Joel McNary ]
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, to flip vertically should be easiest. Right now, your outer loop goes from 1 to 10. What happens if your outer loop goes backwards, from 10 to 1? Give it a try and find out.

Then, you have the slightly harder problem of right-aligning the things. Here, remember, that you have to print spaces before printing the asterisks. How many spaces? Well in the first line, there's 9 spaces and 1 asterisk., in the second line there's 8 spaces and 2 asterisks. Looks like the relationship here is (10 - lineNo) spaces and (lineNo) asterisks. Sounds like a job for two separate loops nested inside the outer loop.

Finally, you have to vertically flip the right-aligned output. That should be simple, now...we've already solved that problem before.

Hope that this helps.
 
Dennis Myhand
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Well, to flip vertically should be easiest. Right now, your outer loop goes from 1 to 10. What happens if your outer loop goes backwards, from 10 to 1? Give it a try and find out."

I gave it a try, as I stated in the long post. It does not work. I really don't know another way to say this. It does not work.


"Sounds like a job for two separate loops nested inside the outer loop"

This is what I have been asking how to do since my first post. I am not understanding how to do this, and I really don't know any other way to say that. I am sorry if I seem irritated, but I find very little help in every answer to my posts saying things like "This should be easy." Perhaps it should be easy. I do not find it so. If what I have asked is beyond the scope of this forum, PLEASE LET ME KNOW.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

As you can see from the commented out section, I have tried adding another loop, I have tried ++line_no, I have tried ++stars, I have tried line_no--, I have tried stars--, I have tried --line_no and --stars. I am at a loss as to what to try next.



I think the problem is that you probably are not sure how the original code works... did you happen to write it? It is not as simple as changing a variable, or changing a postfix to prefix, etc.

I suggest you figure out how the original code works, and then change the program to do the other triangles. Once you have 4 separate programs that does the 4 different triangles, and understand it, then put it together as one program.

Henry
 
Dennis Myhand
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, since I have yet to get anything even remotely close to a striaght answer, how do I contact a moderator to close or delete this topic?
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to nest loops


Your code above (as you probably already know) prints

*
**
***
****
*****
******
*******
********
*********
**********

It looks like you already have the basics of a nested for loop.

Now, after you finish your first outer for loop, you need to run a new outer loop from 0 to 10 while running the inner loop from (10 - line_no) to 0, decrementing stars by one each time.

Does that help?

By the way, I think it would be easier for you if you just print the star each time and do a println at the end of the loop rather than concatenating the stars and printing the whole line.
[ September 19, 2005: Message edited by: Marilyn de Queiroz ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dennis Myhand:
[QBI gave it a try, as I stated in the long post. It does not work. I really don't know another way to say this. It does not work.

[/QB]



As others have pointed out, you do NOT need extra nested loops. Perhaps it will help if you write four separate programs to do each triangle. One you figure out how each one works, then you can combine it into a single program. I think Joel's hints should help you getting one of them at least.

Layne
[ September 19, 2005: Message edited by: Layne Lund ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dennis Myhand:
Okay, since I have yet to get anything even remotely close to a striaght answer, how do I contact a moderator to close or delete this topic?



You can delete the post yourself if you want. But what is the point? As you can see we are more than willing to help, but I think we are helping as much as we can without just giving you the answer. If you are really interested in learning how to program in Java, you should appreciate that self-discovery is part of the learning process. I hope my comments above will help clarify some of the things you are having problems with. The main point is that you only need two nested loops for each triangle. However, each pair does not need to be nested in any of the others as you have already tried.

Keep Coding!

Layne
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Okay, since I have yet to get anything even remotely close to a striaght answer, how do I contact a moderator to close or delete this topic?



By straight answer you mean the exact code of my homework assignment then you are correct. However you have been given enough hints to make some progress and come back with something more specific than "it doesn't work". In particular the suggestion to try it in four seperate programs was a good one, and the one to start a loop at 10 and decrement is very key. Do you know how to do this kind of loop? Something like

will come in handy!

Don't give up. I remember doing a similar problem when I was learning, I'm sure most of us have. The joy comes in figuring it out and seeing the solution that seemed so hard at one point.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic