Java - calculates average of however many test scores user enters

Description

calculates average of however many test scores user enters

Demo

import java.util.Scanner;


public class TestAverage {

  public static void main(String args[]) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter test scores (-1 at the end)");

    int total = 0;
    int numberOfTests = 0;
    int testScore = keyboard.nextInt();

    while (testScore != -1) {
      total = total + testScore;//from w  w  w  . j  av a  2  s .c o m
      numberOfTests++;
      testScore = keyboard.nextInt(); // if didn't have this line then would be infinite loop
    }

    System.out.println("Average" + total / numberOfTests);
    keyboard.close();
  }
}

Related Example