Determining Largest and Smallest integers, without creating multiple ints
I created a program that reads a list of numbers (doubles) terminated by a sentinal value, and outputs the average. The do-while statement with the sentinal value termination uses only one specified integer, changed with the input of each new number. Now without specifying more integers is there a way i can determine the largest and smallest number on the list?
import java.util.*;
publicclass LabThree
{
publicstaticvoid main(String[] args)
{
Scanner keyboard =new Scanner(System.in);
int totaldoubles, max, min;
double total, nextnum;
String answer;
do
{
System.out.println();
System.out.println("Enter a list of nonnegative integers");
System.out.println("When you are finished with the list, enter a negative number");
System.out.println();
System.out.println("I will tell you the largest and smallest integer");
System.out.println("and give you the average the list");
totaldoubles = 0 ;
total = 0;
nextnum = keyboard.nextDouble();
while (nextnum >= 0)
{
total = total + nextnum;
totaldoubles++;
nextnum = keyboard.nextDouble();
}
if (totaldoubles > 0)
System.out.println("The average is: " + (total/totaldoubles));
else
System.out.println("No scores to average");
System.out.println();
System.out.println("Do you want to average another list?");
System.out.println("Enter Yes or No");
answer = keyboard.next();
}while (answer.equalsIgnoreCase("yes"));
}
}
Message was edited by:
thecynicle1
Message was edited by:
thecynicle1

