|
Adding numbers in a 2 dimensional array
Hey guys I'm new to java and I am looking for some help. I want to create a program that will add up the numbers in a two dimensional array. I have most of the code down but I keep getting errors.
Here's my code:
class averageNum {
// initialize # of rows
int[][] arr = { { 1, 0, 12, -1 },
{ 7, -3, 2, 5 },
{ -5, -2, 2, 9 }
};
int x;
int sum;
public int sum(int num)
{
int total = 0;
}
public int test(int sum){
for (int i = 0; i < 3; i++)
for (int j = 0; j < 4; i++)
sum = sum + arr[i][j];
}
public static void main (String[] args){
System.out.println(sum);
}
I get an error in my main method saying "non static variable sum cannot be referenced from a static context"
Any help would greatly be appreciated!
|