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

[2593 byte] By [thecynicle1a] at [2007-10-3 7:14:26]
# 1
Just keep track of a greatest and smallest variable. PseudoCode:while the next number is greater than zeroif the next number is less than smallest, smallest = inputif the next number is greater than greatest, greatest = input
CaptainMorgan08a at 2007-7-15 2:10:49 > top of Java-index,Java Essentials,Java Programming...
# 2
Ok but how to i define the smallest and the largest in the first place to compare the new numbers to?
thecynicle1a at 2007-7-15 2:10:49 > top of Java-index,Java Essentials,Java Programming...
# 3
Well, you could make largest extremely low and smallest extremely high. Or you could do one round of input outside of the loop and assign both values to that.
CaptainMorgan08a at 2007-7-15 2:10:49 > top of Java-index,Java Essentials,Java Programming...
# 4

ok i tried this:

import java.util.*;

public class LabThree

{

public static void main(String[] args)

{

Scanner keyboard = new Scanner(System.in);

int totaldoubles;

double total, nextnum, max, min;

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();

max = nextnum;

min = nextnum;

while (nextnum >= 0)

{

total = total + nextnum;

totaldoubles++;

nextnum = keyboard.nextDouble();

if (nextnum > max)

max = nextnum;

else if (nextnum < min)

min = nextnum;

}

if (totaldoubles > 0)

System.out.println("The average is: " + (total/totaldoubles));

System.out.println("The largest number was: " + max);

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"));

}

}

But now the compiler is saying that the underlined "else" is without an if

Message was edited by:

thecynicle1

Message was edited by:

thecynicle1

Message was edited by:

thecynicle1

thecynicle1a at 2007-7-15 2:10:49 > top of Java-index,Java Essentials,Java Programming...
# 5
If there are no braces following an if statement, the compiler thinks that only the next line belongs to the if. Since you have two statements after the if (and no braces), the compiler thinks you are done with that if.
CaptainMorgan08a at 2007-7-15 2:10:49 > top of Java-index,Java Essentials,Java Programming...
# 6
Yeah i realized that after i posted, i got it all working now, thank you.
thecynicle1a at 2007-7-15 2:10:49 > top of Java-index,Java Essentials,Java Programming...
# 7
No problem.
CaptainMorgan08a at 2007-7-15 2:10:49 > top of Java-index,Java Essentials,Java Programming...
# 8
always 'brace' yourself against such errors :)
mchan0a at 2007-7-15 2:10:49 > top of Java-index,Java Essentials,Java Programming...