/** * Matt Kretchmar * February 3, 2006 * PrintAverage3.java * This program computes the avereage of integers read from * the user. It uses a while loop to read in the values. The * user enters numbers until a -1 is entered as a sentinnel * to indicate end of input. */ import java.util.Scanner; class PrintAverage3 { public static void main (String [] args) { Scanner keyboard = new Scanner(System.in); double average = 0; int input = 0; int i; //loop control variable i = 0; // loop counter initialization // but no real loop initialization while ( input != -1 ) // loop condition { // loop body System.out.println("Enter num" + (i+1) + ": "); // loop update input = keyboard.nextInt(); if ( input != -1 ) { average = average + input; i++; // update counter, but not loop update! } } if ( i > 0 ) average = average / (double)i; System.out.println("average of "+i+" numbers = "+average); } }