• 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 Counter

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
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 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I am a man of mystery. Mostly because of this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic