Source of ExamAverager.java


  1: 
  2: import java.util.*;
  3: 
  4: /**
  5:  Determines the average of a list of (nonnegative) exam scores.
  6:  Repeats for more exams until the user says she/he is finished.
  7: */
  8: public class ExamAverager
  9: {
 10:     public static void main(String[] args)
 11:     {
 12:         System.out.println("This program computes the average of");
 13:         System.out.println("a list of (nonnegative) exam scores.");
 14:         double sum;
 15:         int numberOfStudents;
 16:         double next;
 17:         String answer;
 18:         Scanner keyboard = new Scanner(System.in);
 19:         do
 20:         {
 21:             System.out.println( );
 22:             System.out.println("Enter all the scores to be averaged.");
 23:             System.out.println("Enter a negative number after");
 24:             System.out.println("you have entered all the scores.");
 25:             sum = 0;
 26:             numberOfStudents = 0;
 27:             next = keyboard.nextDouble( );
 28:             while (next >= 0)
 29:             {
 30:                 sum = sum + next;
 31:                 numberOfStudents++;
 32:                 next = keyboard.nextDouble( );
 33:             }
 34:             if (numberOfStudents > 0)
 35:                 System.out.println("The average is "
 36:                                      + (sum/numberOfStudents));
 37:            else
 38:                 System.out.println("No scores to average.");
 39:             System.out.println("Want to average another exam?");
 40:             System.out.println("Enter yes or no.");
 41:             answer = keyboard.next( );
 42:         }while (answer.equalsIgnoreCase("yes"));
 43: 
 44:    }
 45: }