public class WeeklyTemps
2: import java.util.Scanner;
4: /**
5: * A program to read seven daily temperatures, find their average, and
6: * then print them back out again, saying also how far off the average
7: * they were.
8: *
9: * @author Mark Young (A00000000)
10: */
11: public class WeeklyTemps {
13: public static final int DAYS_PER_WEEK = 7;
15: public static void main(String[] args) {
16: // introduce self
17: System.out.println("\n"
18: + "This program reads in a week's worth of daily highs "
19: + "and prints out\nhow they differ from the average.\n");
21: // create variables
22: Scanner kbd = new Scanner(System.in);
23: double[] temps = new double[DAYS_PER_WEEK];
24: double sum = 0.0;
25: double ave;
27: // read and sum the temperatures
28: System.out.println("Enter " + temps.length
29: + " daily high temperatures below:");
30: for (int i = 0; i < temps.length; i++) {
31: temps[i] = kbd.nextDouble(); // read temp
32: sum += temps[i]; // add it to sum
33: }
34: kbd.nextLine(); // tidy up input stream
36: // calculate the average
37: ave = sum / temps.length;
39: // print the temps along with their difference from the average
40: System.out.print("\nDay\tHigh\tDifference");
41: System.out.print("\n---\t----\t----------\n");
42: for (int i = 0; i < temps.length; i++) {
43: System.out.printf("%3d %8.1f %12.1f%n",
44: i, temps[i], (temps[i] - ave));
45: // System.out.println(i + "\t" + temps[i] + "\t" + (temps[i] - ave));
46: }
47: System.out.println();
49: // report average
50: System.out.printf("%3s %8.1f%n",
51: "Ave", ave);
52: System.out.println();
53: }
55: }