【问题标题】:Hi, given a 6x6 2D array, and I have to print the largest sum among all the hourglasses in the array嗨,给定一个 6x6 二维数组,我必须打印数组中所有沙漏中的最大总和
【发布时间】:2022-06-17 02:58:35
【问题描述】:

public static void main(String[] args) { int[][] myArray = new int[6][6];

    for (int i = 0; i < myArray.length; i++) {

        for (int j = 0; j < myArray[i].length; j++) {
            Scanner sc = new Scanner(System.in);
            String input = sc.nextLine();

            String[] arrOfInput = input.split(" ", 6);
            int num = Integer.parseInt(arrOfInput[j]);

            myArray[i][j] = num;


        }
    }

    int maxSum = Integer.MIN_VALUE;
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            System.out.print(myArray[i][j] + " ");

            int sum = myArray[i][j] + myArray[i][j+1]+myArray[i][j+2]
                    +myArray[i+1][j+1]
                    + myArray[i+2][j]+myArray[i+2][j+1]+myArray[i+2][j+2];
            if (maxSum < sum) {
                maxSum = sum;
            }
        }

    }
    System.out.println(maxSum);

在第二行之后,我在线程“main”java.lang 中收到异常。错误。你能帮忙解释一下为什么会这样吗?谢谢!

【问题讨论】:

  • 请提供完整的例外文本。

标签: java multidimensional-array hourglass


猜你喜欢
  • 2016-10-27
  • 1970-01-01
  • 2021-11-14
  • 1970-01-01
  • 2014-12-27
  • 2011-07-07
  • 2013-09-12
  • 2013-07-26
相关资源
最近更新 更多