+ 1

PLEASE help me with a code

Write a program which accepts non-negative double type of numbers as income amounts one by one until a negative number is entered, and the negative number ends the program. When the program is completed by entering a negative number, it prints out the minimum, average, and maximum for the set of entered incomes(excluding the last negative number). For the average, min, max , you always show at most two decimal numbers. Assume the largest number can be entered by user is 1,000,000.00. What I have so far: public static void main(String [ ] args) { Scanner sc = new Scanner(System.in); System.out.print(Enter an income (any negative number to quit:"); double sum = 0; double max = 1,000,000.00; double min = Double.MAX_VALUE; int count = 0; double nextDouble; while (sc.hasNext( ) ) { next Double = sc. nextDouble( ) ; max = Math. max (max, nextDouble); if ( nextDouble < 0) { break; }else { min = Math. min(nextDouble, min); sum += nextDouble; count++; } } System.out.println("Average: " + sum/count); System.out.println("Min: " + min); System.out.println("Max: " + max); } }

18th Apr 2017, 8:48 PM
Philip
3 ответов
+ 9
one way we could better help you is if you post the code you already have in the Code Playground. That way we can see where your stuck and help you out. If you do that you learn more than if we do the code for you. We are here to learn programming isn't it?
18th Apr 2017, 9:04 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 7
-You forgot a starting quotation mark in your first System.out.println(" (Line 2) -max should start at Double.MIN_VALUE, otherwise it'll always be that 1million value. - put max = Math.max... etc in the else statement, so when the user inputs a negative number the negative number isn't counted. (it can make a difference if it's the only input) - accidentally added a space between next Double, change to nextDouble. (Line 9) Other than that, it works for me. just need to do up to 2 decimal places.
18th Apr 2017, 10:31 PM
Rrestoring faith
Rrestoring faith - avatar
+ 6
Here's steps to solve the easiest way, try it yourself first. * Prepare the scanner * Loop * Take input, error check to assure it's a number * If negative, exit loop * add input to list * on loop end, find min, max, and average of the list. Could just use linear searching. * use String.format("%.2f", variable); to sort to 2 decimal places, or use a decimalFormat
18th Apr 2017, 9:03 PM
Rrestoring faith
Rrestoring faith - avatar