/* * This program inputs a sequence of scores, each an integer * from 0 to 10. It drops the lowest score and outputs the * average of the remaining scores. * * TODO: We do not check for scores that are out of range, * and we do not correctly handle the case of 0 or 1 scores. */ import javax.swing.*; public class OlympicScoring { public static void main(String[] args) { final int MAX_VALID_SCORE = 10; // maximum valid score final String INPUT_SENTINEL = "quit"; // our sign to quit int total = 0; // sum of scores int count = 0; // number of scores seen int min = MAX_VALID_SCORE + 1; // minimum so far (sentinel) int score; // the current input value boolean isDone = false; // are we done? do { // repeat until sentinel seen String inputString = JOptionPane.showInputDialog( "Enter a score\n (Enter " + INPUT_SENTINEL + " to terminate) " ); if ( inputString.equals( INPUT_SENTINEL ) ) { isDone = true; // we are now done } else { score = Integer.parseInt( inputString ); total += score; count++; if ( score < min ) // update minimum if needed min = score; // debug print statement System.out.println( "total=" + total + " count=" + count + " min= " + min); } } while ( ! isDone ); total -= min; // remove minimum from total count--; // ...and remove from the count // debug print statement System.out.println( "Revised: total=" + total + " count=" + count); double average = (double) total / (double) count; JOptionPane.showMessageDialog( null, "Average score: " + average ); System.exit( 0 ); // terminate swing } }