/* using a marker to hold a value that meets particular criteria */ /* using an accumulator to hold the sum of a series of values */ /* this time we use a sentinel so the user can stop when s/he runs out of data! */ import javax.swing.JOptionPane; public class MaxNumber_3 { public static void main( String[] args ) { int get_number = 0; int total = 0; int max_number = 0; // Prompt the user to enter a number: String numberString = JOptionPane.showInputDialog(null, "Enter a number: (put in -1 to exit) ", "Input Number Window", JOptionPane.QUESTION_MESSAGE); // Convert the string into an int value get_number = Integer.parseInt(numberString); // use -1 in this case as the sentinel while (get_number != (-1) ) { System.out.println(" The number you entered is " + get_number + "."); if (get_number > max_number) // checking the current value against the marker max_number = get_number; total = total + get_number; // adding to the accumulator variable // Prompt the user to enter another number: String numString = JOptionPane.showInputDialog(null, "Enter a number: (put in -1 to exit) ", "Input Number Window", JOptionPane.QUESTION_MESSAGE); // Convert the string into an int value get_number = Integer.parseInt(numString); } // end of while loop System.out.println("\nThe highest number you entered is: " + max_number); System.out.println("\nThe total of the numbers you entered is: " + total); System.exit( 0 ); } // end main } // end of program