Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# Nested Loop Counter

Sarah Creech
Greenhorn
Posts: 6
I really need some help with this programming assignment. I am suppose to ask a user for a number and from there you are suppose to output the counter in rows of 5 numbers per row.

1 2 3 4 5
6 7 8 9 10
11 12 13 14 and so on I am stuck on the coding part of this this is what I have so far.

// This program is a nested loop that takes in a whole number and counts to what ever number you
// would like it to and puts the numbers into columns.

// Sarah Creech
// October 18, 2004

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

int ctr = 1;
int sum;

System.out.println("Please enter any whole number that you would like me to count to.");

do
{
System.out.print(ctr + " ");
ctr++;
} while(ctr <= sum);
}
}

Anyones help will be appreciated.

fred rosenberger
lowercase baba
Bartender
Posts: 12123
30
well, from the subject of this post, you probably know you need some kind of loop inside another loop.

If i were you, i'd not try and do it all at once. You've got the input working, i assume. that's a good start.

now look at what you need to output... see if you can analyze it for any patterns...

what i see is that the first number of every line increments by 5 each time. Can you write a single loop that only prints out the
1
6
11...

etc. up to your input number? try and get that to work first. once you have that working, we can work on the next part...

Jeff Bosch
Ranch Hand
Posts: 805
Hi, Sarah -

I'd like to recommend that you start your design with pseudocode, not with coding. If you can make your design work with pseudocode, you'll be able to "port" it to Java. For example, a nested loop could look like this:

Using pencil and paper, you can see that your output will be:

11 12 13 14 15
21 22 23 24 25 etc.

Now, once you have the pseudocode working, build your Java code from it.

See how that follows the pseudocode? You could even use your pseudocode as comments for the Java, but I'll leave that up to you to work out.

Let me know if that helps, or if you need more.

Francis Siu
Ranch Hand
Posts: 867
hi Sarah Creech
You can take a look with a similar topic that we have discussed before, I predict that you may know each other.
Of course, some rachers gave you some tips and you just require to edit a little bit code that will become an answer.
My suggestion will be that keep the program as simple as possible.
Do you think that a loop is enough?

[ October 18, 2004: Message edited by: siu chung man ]

Sarah Creech
Greenhorn
Posts: 6
Thanks Guys for all of the help I finally got it working.
I did not know that anyone had replied because it usually emails me and notifies you when someone does answer your question. Dont know why it did not this time. Thanks everyone for your help.