| Author |
printing a value after a sentinel
|
Bob Joness
Greenhorn
Joined: May 20, 2004
Posts: 4
|
|
Hi, I'm currently working through a book called Java By Dissection (as recommended by my Uni) and i've got really stuck on one of the exercises. It asks to write a program that reads in integers until a sentinel value of 0 is entered. This is no problem. However it then says that after the sentinel value is entered that the program should print out the smallest number, other than the 0, that was entered by the user. At the moment the only way i can see of doing this is by creating an array to store the integers that are input by the user in order to search for the smallest value. However as arrays have not yet been introduced in the book i guess they don't expect u to use that method. Each time a new integer is entered the old one gets overwritten due to the fact i only have 1 variable to store it. So how could i get the program to search for the smallest value? There is no limit on the amount of integers to be input otherwise i would create a variable for each integer. Does anyone have any ideas on this? thanks
|
 |
Joel McNary
Bartender
Joined: Aug 20, 2001
Posts: 1815
|
|
Think about this: What would happen if you had a variable named smallestIntSoFar? What would that do for your program? Let us know if this was the hint that you needed, or if you are stuck on something else.
|
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
Don't look at this until after pondering Joel's hint. Second hint, if needed: Consider this input sequence:What values would go in the smallestIntSoFar column? What steps did you take to figure that out? Success?
|
[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
|
 |
Bob Joness
Greenhorn
Joined: May 20, 2004
Posts: 4
|
|
|
Thanks for the help, i figured it out at last and it works. Today i saw the problem i was having in a whole new light and sorted it out in no time.
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
|
Glad to hear it.
|
 |
 |
|
|
subject: printing a value after a sentinel
|
|
|