aspose file tools*
The moose likes Beginning Java and the fly likes nesting loops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "nesting loops" Watch "nesting loops" New topic
Author

nesting loops

doburomirushii nikku
Ranch Hand

Joined: Oct 06, 2004
Posts: 31
I wanted to print out

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

But I want to use a nested loop.
Here's my code I used.

Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
Your code mearly prints "*" six times for every loop of x, and loops x 7 times. Think about what x and y mean. If y means row, how many times do you want it to run through? If x means collumn, how many times do you want it to run through, as a function of the row? You have the current row, y. Remember, that even if you're printing z[100000][400000], it will print it at the current cursor position.


I've heard it takes forever to grow a woman from the ground
doburomirushii nikku
Ranch Hand

Joined: Oct 06, 2004
Posts: 31
I kind of understand you. But, how can I make it so it wont break and just print

*
*
*
and so on... I can code this program in pascal, and I use a similar loop, however I can use gotoxy(y,x) to control where the "*" is placed..Is there something similar for java?
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
well, recall what you are typing...

System.out.println(String). print line. if you don't want to return, use System.out.print(String).
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi Kenshin Himura
Nick George has given a big hint as you require to use the print method in the inner for loop

and I give you another hint that you require to use a println method wihtin the outer for loop



Francis Siu
SCJP, MCDBA
doburomirushii nikku
Ranch Hand

Joined: Oct 06, 2004
Posts: 31
ok thanks.

now I have.

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

I'm trying to get it so the writing of the stars have a relationship with the array, so It will draw the picture I want, or is there a more reasonable way to do this?
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
You now have the x loop looping 5 times for every row. Is this what you want?
Do me a favor, and define for me f(row), such that f(row)=number of columns.
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
ok, another hint
We can use our's logic to think about how to use for loop
For the first line, print five stars
and the second line, print four stars
third line, print three stars
last line, print 1 stars.
We can see the relationship between number of line and number of star that you need to print it out. So the the y variable in the outer for loop has close relationship with the x variable in the inner for loop.
We can think about y=number of line and x=number of stars. If you know how to establish the relationship, then it works.
[ November 07, 2004: Message edited by: siu chung man ]
doburomirushii nikku
Ranch Hand

Joined: Oct 06, 2004
Posts: 31
First of all thanks for all the help.

Here's what I have. What you wrote makes sense to me siu chung man. I'm having trouble putting it into java I guess.



Here's how I'm reading this for y = 1 , x = 5, print out *****. Break. For y = 2, x = 4, print out ****. Break. So on...

This is obviously wrong.
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
Um..........
I could give you an answer but it must be better that you do the assignment by yourself, as I mentioned before, could you try to establish the relationship between the following two nested loop?
for ( int y = 1; y < 6; y++ )
for ( int x = 5; x > 0; x-- )

I give you another example then you can convert the idea to get the answer from the original question
Do you know how to print the following pattern?
*
**
***
****
*****
If you know, then you can solve your question successfully.
The relationship is x=y in the above example. Of course, you need to convert your code from the starting point x=1 to x=5 and y=x.........

[ November 07, 2004: Message edited by: siu chung man ]
doburomirushii nikku
Ranch Hand

Joined: Oct 06, 2004
Posts: 31
Yes..But isn't that what I did in my code? Switched the x and th y around?
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I don't think that is the point that siu is trying to make. Take a look at each row. How many *'s do you want to print in row 1? How many in row 2? (and so on) Can you see a relationship between the row number and the number of *'s in the row? How can this relationship help you decide how many times to execute the inner loop?

Try answering these questions. I hope it will give you an insight into finishing this program (if you haven't done it already, that is).

Good luck!

Layne

p.s. This program doesn't require an array. I see that you aren't using it in your for loop any more, so you might as well remove the declaration as well.


Java API Documentation
The Java Tutorial
doburomirushii nikku
Ranch Hand

Joined: Oct 06, 2004
Posts: 31
You know..sometimes my head just does not work for programming. Thanks for all your help guys, I got it working. Layne cleared up what the others where trying to get through my thick head hehe . Here's the code if your interested.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: nesting loops
 
Similar Threads
The "--" and "-" operators
Doubt on KB Question #12 Chapter 5
Chapter 5 K&B -Doubt
label continue
int cannot be dereferenced