This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Nested Loop Counter

 
Sarah Creech
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.");

sum = SavitchIn.readLineInt();



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

Anyones help will be appreciated.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12185
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
You can click here
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic