
public class SimpleStats {

   // This program reads a set of positive numbers from the user.
   // It then prints out several statistics about the numbers
   // that were entered.

  public static void main(String[] args) {

     Console console = new Console();  // console for I/O with user

     double num;        // one number entered bythe user

     double countNums;  // number of numbers in the data set
     double sum;        // sum of all the data values
     double squareSum;  // sum of the squares of all the numbers
     double max;        // largest number in the data set
     double min;        // smallest number in the data set

     countNums = 0;     // initialize variables
     sum = 0;
     squareSum = 0;
     max = 0;  // (Note: initial values of max and min are not
     min = 0;  //     used, but are required by Java's paranoia)

     console.putln("This program will compute various statistics");
     console.putln("for a set of positive numbers that you enter.");
     console.putln("Enter your data, one number on each line.");
     console.putln("To end, enter any number less than or equal to zero:");
     console.putln();

     do {  // read and process one number
         console.put("? ");
         num = console.getlnDouble();
         if (num > 0) {  // add num to dataset by updating variables
            countNums++;
            sum += num;
            squareSum += num*num;
            if (countNums == 1) {  // is this the first number?
               max = num;
               min = num;
            }
            else {
               if (num > max)
                  max = num;
               if (num < min)
                  min = num;
            }
         }
     } while (num > 0);

     if (countNums == 0) {
        console.putln("OK, so you don't have any data.  Goodbye.");
        console.close();
        return;  // end program by returning from main() routine
     }

     double mean = sum / countNums;  // compute the mean
     double sd = Math.sqrt( squareSum/countNums - mean*mean ); // compute the standard deviation

     console.putln();
     console.putln("Here are some statistics about your numbers:");
     console.putln();
     console.putln("Number of data entries: " + countNums);
     console.putln("         Average value: " + mean);
     console.putln("    Standard Deviation: " + sd);
     console.putln("         Maximum value: " + max);
     console.putln("         Minimum value: " + min);
     console.putln();
     console.close();
  }

}