• 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

nesting loops

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wanted to print out

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

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

 
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
doburomirushii nikku
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 815
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, recall what you are typing...

System.out.println(String). print line. if you don't want to return, use System.out.print(String).
 
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
doburomirushii nikku
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 815
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes..But isn't that what I did in my code? Switched the x and th y around?
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
doburomirushii nikku
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
And then the entire population worshiped me like unto a god. Well, me and this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic