import java.util.Scanner; public class Prog5_9 { public static void main(String[] args) { System.out.println("Enter a list of numbers and I will tell you the two largest"); Scanner in = new Scanner(System.in); int current, largest1 = 0, largest2 = 0; // make the assumption that all input numbers will be greater than or equal to 0 System.out.print("Enter the first number, a negative number to end the input: "); current = in.nextInt(); while(current >= 0) // iterate while the user is entering numbers >= 0 { if(current > largest1) // if the new number is greater than the previous largest then { largest2 = largest1; // move the largest into the second largest and remember the new number as largest largest1 = current; } else if(current > largest2) // otherwise if the new number is greater than the second largest then largest2 = current; // replace second largest with the new number System.out.print("Enter the next number, a negative number to end the input: "); // get the next input current = in.nextInt(); } // output the results System.out.println("The two largest numbers entered are " + largest1 + " and " + largest2); } }