/** * This program will compute the arithmetic average for a set of exam scores . * Any negative value will be used as a sentinel * * @author Anthony Larrain */ import java.text.DecimalFormat; import java.util.Scanner; class AverageExamScore { public static void main( String [] args ) { // declare and initialize primitive variables int sum = 0, count = 0; // declare and create a Scanner object Scanner input = new Scanner(System.in); System.out.println("Enter first score ( enter a negative number to quit)"); int score = input.nextInt(); while (score > -1){ sum += score; ++count; System.out.println("Enter next score ( enter a negative number to quit)"); score = input.nextInt(); } if(count != 0){ double average = sum / (double) count; DecimalFormat fmt = new DecimalFormat("0.00"); System.out.println("The average is : " + fmt.format(average)); }else{ System.out.println("No scores entered"); } } }