// Project: Loops Example // Module: stats // Compute the count, sum, average, min, // and max for a list input from the keyboard. import java.util.Scanner; public class Main { public static void main(String[] args) { // Create Scanner object. Scanner input = new Scanner(System.in); // Show prompt for input list. System.err.println("Enter input list, one value per line."); System.err.println("Terminate list with Control-Z"); // Declare and initialize accumulator variables. int count = 0; double value = 0.0, sum = 0.0, ave = 0.0, min = Double.MAX_VALUE, max = -Double.MAX_VALUE; // Process list, updating accumulator variables. while(input.hasNextLine( )) { value = Double.parseDouble(input.nextLine()); count++; sum += value; min = Math.min(min, value); max = Math.max(max, value); } // Output stats. System.out.println("Length of list: " + count); System.out.println("Sum of list: " + sum); System.out.println("Average of list: " + sum / count); System.out.println("Minimum of list: " + min); System.out.println("Maximum of list: " + max); // Close scanner. input.close( ); } }