import java.io.*; //------------------------------------------------------------------- // Program P2 Q4 // // This program prompts users for a series of positive numbers, and // calculates the mean, maximum and minimum of these numbers. // // Author : Wei Tsang Ooi // Date : 11 July 1999 //------------------------------------------------------------------- class MeanMaxMin { static double sum = 0; // sum of all values entered so far static double count = 0; // how many numbers is entered so far static double min = Double.MAX_VALUE; // minimum so far static double max = Double.MIN_VALUE; // maximum so far //--------------------------------------------------------------- // intro // // print an introduction message. //--------------------------------------------------------------- private static void intro() { System.out.print("I will ask for a series of numbers, and " + "find out the average value, minimum value " + "and maximum value.\n"); } //--------------------------------------------------------------- // getInput // // input : stdin - BufferedReader to read input from // msg - message to prompt the user with // return : a double value input by user, guranteed to be // greater than zero. //--------------------------------------------------------------- private static double getInput(BufferedReader stdin, String msg) throws IOException { System.out.print(msg); double input = Double.valueOf(stdin.readLine()).doubleValue(); return input; } //--------------------------------------------------------------- // updateStats // // input : input - a number input by users // // This method updates the member min, max, sum, and count . //--------------------------------------------------------------- private static void updateStats(double input) throws IOException { if (input < min) min = input; if (input > max) max = input; sum += input; count++; } //--------------------------------------------------------------- // printStats // // This method prints the mean, min, and max of the numbers // entered by user //--------------------------------------------------------------- private static void printStats() { if (count == 0) System.out.println("Nothing is entered."); else { System.out.println("Here are the statistics :"); System.out.println("Mean : " + sum/count); System.out.println("Min : " + min); System.out.println("Max : " + max); } } //--------------------------------------------------------------- // main // // Keep reading values from user, until a 0 is entered. Then // print out the average, min and max value of the values entered. //--------------------------------------------------------------- public static void main(String argv[]) throws IOException { intro (); BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in)); double input = getInput(stdin, "Enter a number [0 to quit] :"); while (input != 0) { updateStats(input); input = getInput(stdin, "Enter a number [0 to quit] :"); } printStats(); } }