[Method] needing some help
First I want to start explaining what the programs purpose. I am asking the user to input temperatures for an 8 hour span of time. So they input 8 numbers the program then prints those numbers on the screen like so:
Enter temperature for hour 1: .....
Enter temperature for hour 2: .....
Temperature Report
The temperature of hour 1: 68
The temperature of hour 2: 78
etc.
now this is where im running into trouble. I'm trying to get the average of all the temperatures that are in the array which I have the formula for and I think that its correct but whats happening I think has to do with how my for loop is stated and its printing an average number for each single temperature instead of giving me just the average of all the temperatures together. Below is my code and the average part is at the bottom of the code. Any help would be appreciated.
Code Java:
import java.util.*;
public class temperature
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
int[] temp;
temp = new int[8];
double average;
int count;
int sum;
for(count = 0; count <temp.length;count++)
{
System.out.print("Enter temperature for hour " + (count + 1) + " : ");
temp[count] = console.nextInt();
}
System.out.println();
System.out.println();
System.out.println("Temperature Report");
for(count = 0; count <temp.length; count++)
{
System.out.println("Hour " + (count + 1) + " : " + temp[count]);
}
sum = 0;
for(count = 0; count < temp.length; count++)
{
sum = sum + temp[count];
average = sum / temp.length;
System.out.println("Average Temperature: " + average);
}
}
}
Re: [Method] needing some help
You need to specify an index.
Re: [Method] needing some help
Yea, you need to use your index, which in your case is counter.
sum += a[counter];
Dejan
Re: [Method] needing some help
thank you so much for the help. it is greatly appreciated.
Re: [Method] needing some help
problem has been fixed thanks for any previous help I was given.