java - GameOfLife IndexOutOfBounds 错误处理

标签 java error-handling

所以,我正在做一项作业,创建一个类来接收为康威的生命游戏设置的文本文件。我已经写了所有内容,但很难测试,因为我的错误处理能力很差。我已经阅读了关于 try、catch、 throw 等的 java 教程页面。我不明白它,如果我能找到一些解决 IndexOutOfBounds 错误的方法,它会节省我很多时间。

public void computeNextGeneration(int generation) {
    int aliveCount;
    tempBoard = new char[Column][Row];
    int generationCount = 1;
    System.out.print("Generation" + generationCount);
    print();
    do {
        for (int i = 0; i < Row; i++) {
            for (int j = 0; j < Column; j++) {
                aliveCount = 0;
                try {
                    if (board[Row - 1][Column - 1] == 'X') {
                        aliveCount++;
                    }
                    if (board[Row - 1][Column] == 'X') {
                        aliveCount++;
                    }
                    if (board[Row - 1][Column + 1] == 'X') {
                        aliveCount++;
                    }
                    if (board[Row][Column - 1] == 'X') {
                        aliveCount++;
                    }
                    if (board[Row][Column + 1] == 'X') {
                        aliveCount++;
                    }
                    if (board[Row + 1][Column - 1] == 'X') {
                        aliveCount++;
                    }
                    if (board[Row + 1][Column + 1] == 'X') {
                        aliveCount++;
                    }
                    if (board[Row + 1][Column + 1] == 'X') {
                        aliveCount++;
                    }
                    if (board[i][j] == 'X') {
                        if (aliveCount < 2) {
                            setCell(j, i, 0);
                        }
                        if (aliveCount > 2) {
                            setCell(j, i, 0);
                        } else {
                            setCell(j, i, 1);
                        }
                    }
                    if (board[i][j] == '0') {
                        if (aliveCount == 3) {
                            setCell(j, i, 1);
                        }
                    }
                } catch (IndexOutOfBoundsException e) {
                }
                throw new IndexOutOfBoundsException();
            }
            board = tempBoard;
            generationCount++;
            System.out.print("Generation" + generationCount);
            print();
            System.out.println();
            generation--;
        }
    } while (generation > 1);
}

第一种情况,位于二维数组的边缘将给出第一个错误。我想如果我把检查相邻数组索引的代码放在一起......说实话,我只是像在黑暗中拍摄一样将代码放在一起。如果我能得到一个与我的问题类似的示例,任何可以说明处理 IndexOutOfBounds 错误的示例,我都会非常感激。

最佳答案

乘坐 8 if语句脱离了 try-catch 场景,而是专注于检查 board 的索引是否存在。是正确的。换句话说,在访问board[Row - 1][Column - 1]之前,检查0 <= Row-1 <= NumOfRows0 <= Column-1 <= NumOfColumns .

编辑

更明确的 View 。

for (int i = 0; i < NumOfRows; i++) {
            for (int j = 0; j < NumOfColumns; j++) {
                aliveCount = 0;
                if (i-1 >= 0 && i-1 <= NumOfRows) {//This case checks the cell to the left
                    aliveCount++;
                }
                //Other cases

关于java - GameOfLife IndexOutOfBounds 错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20392462/

相关文章:

java - 这是 Swing 中 MVC 的正确实现吗?

java - 二维数组越界异常

php - 在 fopen() 失败时获取有意义的信息 (PHP/suPHP)

c# - ServiceStack 是否需要 ResponseStatus?

php - 如何修复Wamp服务器中的错误 “Uncaught Error: Call to undefined function oci_connect()”?

javascript - JavaScript 抛出 ReferenceError 背后的逻辑是什么?

java - 需要关于通过验证数据阅读大量 XL 的建议

java - 如何修复涉及android接口(interface)的循环继承?

delphi - 我如何从delphi检索WinInet错误代码的错误描述

java - 如何在 Android 中创建我的自定义类?