Please help!!!!!
Hi. I want to print out the sum of an array. This is a simple version of my code can anyone tell me where I'm going wrong? Im getting an error about static reference of sum in main - is there a way aroung this?
publicclass temp{
publicstaticvoid main(String[] args){
double temps[] ={5.1,4.6,7.7,10.9,21.1,16.2};
System.out.println("Total: " + sum);
}
publicdouble sum(double temps[]){
int sum=0;
for(int i=0;i<temps.length;i++)
sum+=temps[i];
return(sum);
}
}
>
[1223 byte] By [
javaLa] at [2007-11-27 4:37:05]

Ok , can I just call the sum method from main?
The error I am getting is:
java:7: non-static variable temps cannot be referenced from a static context
public class temp {
double temps[] = {5.1,4.6,7.7,10.9,21.1,16.2,17.3,17.5,14.2,10.1,8.1,6.2};
public static void main(String[] args){
sum(temps);
}
public void sum(double temps[]) {
double sum=0.0;
for(int i=0;i<temps.length;i++)
sum+=temps[i];
//return(sum);
System.out.println("Sum: " + sum);
}
}
>
WOW!
This
System.out.println("Total: " + sum);
needed to be changed to this
System.out.println("Total: " + sum());
but lets make a whole bunch of changes instead.
In the words of the late great Douglas Adams "DON'T PANIC". Just take a breather and try to think clearly.