• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about organizing an array  RSS feed

 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I did a problem where I have to list 1-5 in order and in reverse with an array (using a for loop). I did it below but can someone explain how to have them listed in order? So instead of:

Your number is: 1
Your number is: 2

it just says "Your number is: 1, 2, 3, 4, 5."

Thanks.

 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your are on the right track, all you have to do is use System.out.print instead of System.out.println as follows:

 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rico. I appreciate it.
 
Miles Williams
Greenhorn
Posts: 23
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's also worth mentioning that as Rico has done in his code, do not use a constant value (NUM_AMOUNT), instead opt for the use of the length property of your array in your for loops termination condition. This is always a safer bet in the case of array sizes changing. It's far more maintainable. If you decide that you need to add another element to your array, you then have to also change that constant value, which may not be a big deal, but it's best to get into the habit of always coding for easier maintainability.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!