//---------------------------------------------------------------------------------------------
// CS100 P2: Q4
//
// Requests and accepts a series of positive numbers as input from the user.
// The number of values is not known in advance; an input of 0 indicates
// the end of the list. Print the arithmetic mean, the maximum number, and
// the minimum number.
//
// Author : Alan Renaud (ajr5@cornell.edu)
// Date : 15 July 1999
//---------------------------------------------------------------------------------------------
public class listOfNumbers {
static int count; // = number of positive values input by user
static int min; // = smallest value input by user
static int max; // = largest value input by user
static int total; // = running total of values input by user
static int value; // = latest number input by user
//---------------------------------------------------------------------------------------------
// getRightInput
//
// input : none
// return : the value input by user
//
// Prompts user for non-negative values and returns the value of input if input is
// valid. Repeats prompt otherwise.
//---------------------------------------------------------------------------------------------
private static int getRightInput()
{
int val;
TokenReader in = new TokenReader(System.in);
do {
System.out.print("Enter a POSITIVE number, or 0 if you don't want to play: ");
System.out.flush();
val = in.readInt();
}while (val < 0);
return val;
}
//---------------------------------------------------------------------------------------------
// updateVars
//
// input : none
// return : none
//
// Updates values of count, total, min, and max
//---------------------------------------------------------------------------------------------
private static void updateVariables()
{
count++;
total+=value;
if (value < min)
min = value;
else if (value > max)
max = value;
}
//---------------------------------------------------------------------------------------------
// processList
//
// input : none
// return : none
//
// Process the list of numbers input by user. If the user inputs valid values,
// update the relevant variables, end processing otherwise.
//---------------------------------------------------------------------------------------------
private static void processList()
{
while (value!=0)
{
updateVariables();
value = getRightInput();
}
}
//---------------------------------------------------------------------------------------------
// printOutput
//
// input : none
// return : none
//
// Calculate and output to the screen the arithmetic mean, maximum number, and
// minimum number from the list input by the user. Output error message if user
// did not enter any values.
//---------------------------------------------------------------------------------------------
private static void printOutput()
{
TokenReader fini = new TokenReader(System.in);
if (count==0)
System.out.println("\nYou didn't enter any values, so I can't calculate the min, max, or mean.");
else
{
System.out.println("\nThe minimum value entered was " + min + ".\nThe maximum value entered"
+ " was " + max + ".\nThe arithmetic mean was " + (double) total/count);
}
fini.waitUntilEnter();
}
public static void main (String[] args)
{
value = getRightInput();
min=max=value;
processList();
printOutput();
}
}