Well I am trying to learn how to do arrays and I tried to write this program for practice but can't quite get one thing.. My program takes hourly temperatures input from a user and outputs them, outputs their average, and outputs the highest temperature.. what I would like to do is make a for that prints all of the temperatures that are above the average but I can't quite get it.
Anyway here's the code..
Code:
import java.util.*;
public class TemperatureArray
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
int[] temp = new int[8];
int avg = 0;
int index;
int sum = 0;
int hour = 1;
int maxIndex = 0;
System.out.println("Enter 8 hourly temperatures: ");
for (index = 0; index < temp.length; index++)
{
temp[index] = console.nextInt();
System.out.println();
}
System.out.println("Temperature Report");
for (index = 0; index < temp.length; index++)
{
System.out.println("Temperature for Hour " + hour++ + ": " + temp[index] + " ");
}
for (index = 0; index < temp.length; index++)
{
sum = sum + temp[index];
}
if (temp.length != 0)
avg = sum / temp.length;
else
avg = 0;
System.out.println();
System.out.println("Average Temperature: " + avg);
for (index = 1; index < temp.length; index++)
if (temp[maxIndex] < temp[index])
maxIndex = index;
else
System.out.println();
System.out.println("Highest Temperature: " + temp[maxIndex]);
}
}