I am trying to read in a group of numbers from a users input through a console after the sentinel value of 0 is entered then I need to output the largest integer put in and the next largest. I can get the largest but not the next largest. What should I do? See the code for the largest integer below. All numbers entered should be positive.
/*
* File: LargestInt.java
* -----------------------
* This program reads integers one per line until the
* user enters a special sentinel value to signal the
* end of the input. At that point, the program
* prints the largest integer entered.
*/
import acm.program.*;
public class LargestInt extends ConsoleProgram {
public void run() {
println ("This program prints the largest integer entered.");
println ("Enter values, one per line, using "+SENTINEL);
println ("to signal the end of the list.");
int largest = 1;
int number = readInt(" ? ");
while (number != SENTINEL) {
number = readInt(" ? ");
if (number>largest){largest=number;}
}
println("The largest number was: "+largest);
}
/* Specifies the value of the sentinel */
private static final int SENTINEL = 0;
}
Thanks in advance,
Michael