// Marty Stepp, CSE 142, Autumn 2008 // This program prompts the user to enter several days' worth of temperatures // and computes the average temperature and how many days were above average. // This version of the program is able to compute how many days were above // average by storing all temperatures into an array. import java.util.*; // for Scanner public class Weather2 { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("How many days' temperatures? "); int days = console.nextInt(); int sum = 0; int[] temperatures = new int[days]; // read each day's temperature into the array // 45 44 39 48 37 46 53 for (int i = 0; i < days; i++) { System.out.print("Day " + (i + 1) + "'s high temp: "); int temp = console.nextInt(); sum += temp; temperatures[i] = temp; } // compute how many days were above average double average = (double) sum / days; int aboveAverage = 0; for (int i = 0; i < temperatures.length; i++) { if (temperatures[i] > average) { aboveAverage++; } } System.out.printf("Average temp = %.1f\n", average); System.out.println(aboveAverage + " days were above average."); } }