I want to write a program that creates a two-dimensional int array initialized with test data. The program cannot run. I am confused about the code.Please help with the problem.How to correct my code? Thank you.
public class Int2DArray {
private static int x;
private static int y;
public static int getTotal(int[][] numbers) {
int total = 0;
for (int x = 0; x < numbers.length; x++);
for (int y = 0; y < numbers[x].length; y++);
total = total + numbers[x][y];
return total;
}
public static double getAverage(int[][] numbers) {
double average = 0;
average = getTotal(numbers) / (x + y);
return average;
}
public static int getRowTotal(int[][] numbers, int index) {
int total = 0;
for (int y = 0; y < 3; y++) {
total = total + numbers[index][y];
}
return total;
}
public static int getColumnTotal(int[][] numbers, int index) {
int total = 0;
for (int x = 0; x < numbers.length; x++) {
total = total + numbers[x][index];
}
return total;
}
public static int getHighestInRow(int[][] numbers, int x) {
int high = numbers[x][0];
for (int i = 1; i < 3; i++) {
if (numbers[x][i] > high) {
high = numbers[x][i];
}
}
return high;
}
public static int getLowestInRow(int[][] numbers, int x) {
int low = numbers[x][0];
for (int i = 1; i < 3; i++) {
if (numbers[x][i] < low) {
low = numbers[x][i];
}
}
return low;
}
}
public class Int2DArrayTest {
public static void main(String[] args) {
int[][] iarray = {{2, 1, 9}, {7, 3, 4}, {5, 6, 8}};
System.out.println("Total:" + getTotal(iarray));
System.out.println("Average:" + getAverage(iarray));
System.out.println("Total of row 0" + getRowTotal(iarray, 0));
System.out.println("Total of row 1" + getRowTotal(iarray, 1));
System.out.println("Total of row 2" + getRowTotal(iarray, 2));
System.out.println("Total of col 0" + getColumnTotal(iarray, 0));
System.out.println("Total of col 1" + getColumnTotal(iarray, 1));
System.out.println("Total of col 2" + getColumnTotal(iarray, 2));
System.out.println("Highest in row 0" + getHighestInRow(iarray, 0));
System.out.println("Highest in row 1" + getHighestInRow(iarray, 1));
System.out.println("Highest in row 2" + getHighestInRow(iarray, 2));
System.out.println("Lowest in row 0" + getLowestInRow(iarray, 0));
System.out.println("Lowest in row 1" + getLowestInRow(iarray, 1));
System.out.println("Lowest in row 2" + getLowestInRow(iarray, 2));
}
}
getTotal
method, specially the semi colons after each for. – ZouZou Mar 9 at 20:16