• 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

Nested Loop

 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok guys...I have another question for you. I need to write a nested loop. I have to ask a user to input a whole number, and let's say they enter the number 20. I need to output the number in five columns... 1 2 3 4 5
6 7 8 9 10
and so on. I need some sort of loop that counts, and some kind of tab things...any suggestions?
 
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 suggest you start with


Seriously, you will need to use a for loop, more than likely. Do you understand the syntax given in your textbook (assuming you have a textbook, of course)? What have you done so far?

If you aren't sure about where to start, perhaps writing some ideas on paper can get the ideas churning. I often find this helpful when tackling a new problem. Alternatively, you can post your thoughts here, including any bits of code you have tried.

Once you show the effort you have made in writing this program, there are many people here that are more than willing to help out. Please let us know what you come up with as a starting point.

Good luck!

Layne
[ October 17, 2004: Message edited by: Layne Lund ]
 
Rose Evans
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Layne,

As you can see, I started with..public static void main(String[] args)
Just kidding.
So far, I had the user inputting his/her number, and my program is outputting 1 2 3 4 5 6 and so on, but it's on a straight line, I need my numbers to output in columns ... like..
1 2 3 4 5
6 7 8 9 10 and so on up to 20.

Any suggestions? Yes, I have a book, but boy it's a strange book, not too many examples to go by.


Thanks for your reply. It is greatly appreciated.

(Marilyn added code tags)
[ October 18, 2004: Message edited by: Marilyn de Queiroz ]
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm infernig from your sample output that the problem is to output the integers from one to the user's number, five per line. If so, then you're only missing the last part: "five per line."

Hint 1: "System.out.println();" will cause a carriage return so output starts on the next line.

Hint 2: Look up "modulo" or "mod" in your book.
 
Rose Evans
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok...I'll see what I can do now. Thanks so much for your help!!!
 
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 Rose Evans
Did you learn MOD and using if condition?
If you have already learnt these, then you can think about MOD and count using if condition to do it.
There are several methods that can be used in your problem, one of the method is given above.
A big hint
 
Rose Evans
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI siu chung man,

I looked in my java book and I didn't see anything on MOD...is that modulus? I have just got to work, so after work, I'll search around and see if I can find something.
There's some kind of tabs you can set in your code, but I don't know how to do it.
 
Rose Evans
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand that the System.out.println(); will cause a new line, but how to you get it to just stop at the number 5, and then start a new line at 6 to go through 10?
1 2 3 4 5
6 7 8 9 10 and so on?

I am just clueless...
 
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rose...

Try thinking in this direction...your requirement is to get a new line, after number 5 and at intervals of 5.

So, the conditions that we look for to get to the next line should be

1. The number is greater than or equal to 5 - (i>=5)
2. The number is to be a multiple of 5 - (i%5==0)

In case the condition turns true, we go for a println()

else we go for print()

This is the idea to go about.

If you still have problems, just mail me for the code...Hope this helps you out.

Cheers,
Swamy
 
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
Sorry for my unclear statement
Hints:
1: if condition
2: using % (MOD) with your program variable count
If you did not learn % (MOD)
I can talk about what it is
For example
3%2=1, the function of % (MOD) is used to find out the reminder
So 4%2=0, 5%2=1
In your case, if count%??==?? then print out a new line.
:roll:
 
Rose Evans
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Swamy,

I did it!!!Thank you, thank you, thank you!!! Two heads are always better than one!! Tell me, do you program for a living? Or are you a student, or what?

THANK YOU SO MUCH FOR YOUR HELP!!!
 
Rose Evans
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh duh on my part, I see student listed under your name. You seem to be really smart at this java stuff!! How long have you been using java?
 
Thanks tiny ad, for helping me escape the terrible comfort of this chair.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic