#Getting MAX number from user inputs

7 messages · Page 1 of 1 (latest)

waxen star
#

hi i just started to learn java and i got a question to find the max value from user input using scanner.
the first part of the question was to Write a loop that receives numbers from the user (representing prices of dishes in the restaurant) until the sum of the numbers exceeds 500 shekels (or $ it doesn't matter im from israel so..),
the second part of the question was to Capture a number from the user (before the loop) that represented the maximum number, now use this captured number (instead of the 500).
how do i find the max number?

dusky hazel
#

i hope i am getting it right:
u need the price from the scanner before your while-loop right?

#

or do you need the highest price from all the entries done (before and in while)?

#

but i guess it will go in the same direction anyway ...
if u want something compared you have to have 2 variables
in your case, if i am getting it right, u want to compare the prices

#

in your case u are setting the price variable anew with each entry
therefor u can't compare the "old" price (= before loop) with the one in the loop (=new price ==> price = sc.nextInt();)

you would need a new variable where u also store the initial value and then compare it every time with the new entry u get (==> if-statement)

unique onyx
# waxen star hi i just started to learn java and i got a question to find the max value from ...

If I got you right, then:
Your task is to keep track of numbers user has put and:

  1. Sum them up
  2. Get max inputted value

You can solve 2nd task by:

  1. Creating a variable for storing max number
  2. For each inputted number (right after increment of sum) you check: if price > maxNumberVariable. If it is, you change the value of max number variable: maxNumberVariable = price. If not - do nothing.
#

Possible solution:

...
int price = sc.nextInt();
int max = price;
...
while (...) {
    ...
    if (price > 0) {
        sum = sum + price;
        if (price > max) max = price;
    }
    ...
}
...