Source of AverageGasPrice.java


  1: import java.util.*;
  2: 
  3: public class AverageGasPrice
  4: {
  5:         public static void main(String[] args)
  6:         {
  7:                 double[] gasPrice = new double[25];
  8:                 int priceCount = 0;
  9:                 double price = 0;
 10: 
 11:                 Scanner keyboard = new Scanner(System.in);
 12:                 System.out.println("This program calculates and displays the average");
 13:                 System.out.println("price of a regular gallon of gas.");
 14: 
 15:                 System.out.println("Enter the price of gas or -99 to Quit: ");
 16:                 price = keyboard.nextDouble();
 17: 
 18:                 while(price != -99)
 19:                 {
 20:                         gasPrice[priceCount] = price;
 21:                         priceCount++;
 22:                         price = keyboard.nextDouble();
 23:                 }
 24: 
 25:                 //Compute the average
 26:                 double sum = 0;
 27: 
 28:                 for(int i=0; i < priceCount; i++)
 29:                 {
 30:                         sum = sum + gasPrice[i];
 31:                 }
 32: 
 33:                 double average = sum/priceCount;
 34: 
 35:                 System.out.print("\n\nThe average price per gallon of regular");
 36:                 System.out.println(" gas is " + average);
 37: 
 38:         }
 39: }