• 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

Multiple variables in a loop

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
instead of trying to explain all the different conditions I've tried, maybe there's a much simpler approach to the following situation:

I want to be able to input a unknown number of values, ending the list with the value 999
I then want it to take the difference of each consecutive value.
Finally, output these differences in a list.

Example

Input:
10.5
12.05
13.25
21
7
999

Output:
1.55
1.20
7.75
-14

I do not want the outputs list mixed in the inputs list... I want all the inputs listed first (as they're entered - an unknown number of inputs), the list ending with 999.
Followed by a list of outputs...
Any suggestions?

p.s. the equations and code is simple enough, just the formatting of getting all the inputs first then listing all the outputs is where I'm stuck.
 
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
collect all your inputs into something like an Arraylist (which will grow as big as you need it).

once the user inputs 999 (don't collect that value), just iterate through the list and do the subtraction.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to javaranch Brandon
reply
    Bookmark Topic Watch Topic
  • New Topic