• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Reverse numbers using java arrays

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello I am trying to do this assignment but I can't get the needed output. Can any one help me
what I am missing? Thank you so much!
Assignment
Create a program that asks the user how many floating point numbers he wants to give. After this the program asks the numbers, stores them in an array and prints the contents of the array in reverse order.

Program is written to a class called ReverseNumbers.
Example output


How many floating point numbers do you want to type: 5
Type in 1. number: 5,4
Type in 2. number: 6
Type in 3. number: 7,2
Type in 4. number: -5
Type in 5. number: 2

Given numbers in reverse order:
2.0
-5.0
7.2
6.0
5.4

My code:

 
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Your loops should start at 0
2. You're using pre increment and pre decrement operators in the loops.
3. You have an array of doubles but you're reading ints from the scanner
4. On your second for loop, you're looping while i is LESS THAN or equal to zero.
5. You're reading from the keyboard again on your second loop.
 
daniel kidanee
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, charles,
Thanks for all the suggestions. When I was trying to create the code the difficult thing for me was
how do I make the program on the second loop make to read the values in reverse order?
do you have any suggestions? I can fix the rest.Thanks
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

daniel kidanee wrote:how do I make the program on the second loop make to read the values in reverse order?
do you have any suggestions?


Look at the logic of your for loop. You set i to be one less than the length of the array. You then check if i is less than or equal to zero. That will only ever be true for an array of length 0 or 1.
Also, why are you reading numbers in again in the second loop. You already have the numbers, you just need to print them.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Charles D. Ward wrote:2. You're using pre increment and pre decrement operators in the loops.


That makes absolutely no difference at all.

@daniel: This is simply a style point. It's more usual to see post-increments in sample code, but it will have no effect on the way your loop works. Personally, I tend to use pre-increments for most things, since the value of the expression is what the variable will contain until it's next updated, which can be less confusing when you're debugging; but it's simply my preference.

Winston
 
reply
    Bookmark Topic Watch Topic
  • New Topic