JAVA二维数组代码

时间:2014-03-09 20:12:42

标签: java arrays

我想编写一个程序来创建一个用测试数据初始化的二维int数组。   该程序无法运行。我对代码感到困惑。请帮助解决问题。如何更正我的代码?谢谢。

    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));
    }
}

3 个答案:

答案 0 :(得分:2)

您已将Int2DArray内的方法声明为静态

public static int getTotal(int[][] numbers) { //

所以你需要将它们称为静态,以便像以下一样使用它们:

System.out.println("Total:" + Int2DArray.getTotal(iarray));

答案 1 :(得分:2)

我看到一个大错误:

你可以解决它:

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 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;
        }

或更好(使用foreach):

public static int getTotal(int[][] numbers) {
        int total = 0;
        for (int [] x : numbers){
            for (int y : x){
                total = total + y;
            }
        }
        return total;
    }

在IDE中使用格式化热键。


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;
        }

这里你使用常量3 - 你需要将它提取为常量

是一种糟糕的风格
public static double getAverage(int[][] numbers) {
            double average = 0;
            average = getTotal(numbers) / (x + y);
            return average;
        }
  1. x和y未在您的代码中初始化
  2. 为什么要除以x+y。你需要除以元素的数量。

答案 2 :(得分:0)

你有两次跟随行,所以一个明显的语法错误。

 public class Int2DArrayTest {

此外,getAverage函数中也存在逻辑错误。

average = getTotal(numbers) / (x + y);

它显示除零例外。