抛出新的运行时异常问题

jjhzyzn0  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(275)

有人能帮我弄清楚如何实现抛出一个新的runtimeexception吗?我试着将它添加到注解所在的位置,它位于play()方法的顶部,返回false将被删除,因为在抛出异常时不需要它。我得到的错误是“runtimeexception无法解析为类型”。谢谢您。

package BS;

public class Board {
    private char[][] board;
    private char currentPlayer;

    //Initializes the board
    public Board() {
        this.board = new char[7][6];
        for (int i=0; i<7; i++) {
            for (int j=0; j<6; j++) {
                this.board[i][j] = 'Y';
            }
        }
        this.currentPlayer = 'X';
    }

    public char currentPlayer() {
        return this.currentPlayer;
    }

    public boolean play(int column) {
        //Determines which position on the board is free
        if (column < 1 || column > 6) return false; //HERE
        int saveI = 0;
        for (int i=0; i<7; i++) {
            if (this.board[i][column-1] == 'Y') {
                saveI = i;
            }
        }
        //Changes the position to the current player
        if (this.board[saveI][column-1] == 'Y') {
            this.board[saveI][column-1] = this.currentPlayer;
            if (this.currentPlayer == 'X') {
                this.currentPlayer = 'O';
            } else {
                this.currentPlayer = 'X';
            }
            return true;
        }
        return false;
    }

    public boolean gameOver() {
        //Horizontal checks
        for (int i=0; i<7; i++) {
            for (int j=3; j<6; j++) {
                if (this.board[i][j-3]=='X' && this.board[i][j-2]=='X' && this.board[i][j-1]=='X' && this.board[i][j]=='X') {
                    return true;
                }
            }
        }
        //Vertical checks
        for (int i=3; i<7; i++) {
            for (int j=0; j<6; j++) {
                if (this.board[i-3][j]=='X' && this.board[i-2][j]=='X' && this.board[i-1][j]=='X' && this.board[i][j]=='X') {
                    return true;
                }
            }
        }

        //Diagonal Checks

        //Diagonal Top Left to Bottom Right check
        for (int i=3; i<7; i++) {
            for (int j=3; j<6; j++) {
                if (this.board[i-3][j-3]=='X' && this.board[i-2][j-2]=='X' && this.board[i-1][j-1]=='X' && this.board[i][j]=='X') {
                    return true;
                }
            }
        }
        //Diagonal Top Right to Bottom Left check
        for (int i=3; i>7; i++) {
            for (int j=2; j>0; j--) {
                if (this.board[i-3][j+3]=='X' && this.board[i-2][j+2]=='X' && this.board[i-1][j+1]=='X' && this.board[i][j]=='X') {
                    return true;
                }
            }
        }
        //Diagonal Bottom Left to Top Right check
        for (int i=3; i>0; i--) {
            for (int j=3; j<6; j++) {
                if (this.board[i+3][j-3]=='X' && this.board[i+2][j-2]=='X' && this.board[i+1][j-1]=='X' && this.board[i][j]=='X') {
                    return true;
                }
            }
        }
        //Diagonal Bottom Right to Top Left check
        for (int i=3; i>0; i--) {
            for (int j=2; j>0; j--) {
                if (this.board[i+3][j+3]=='X' && this.board[i+2][j+2]=='X' && this.board[i+1][j+1]=='X' && this.board[i][j]=='X') {
                    return true;
                }
            }
        }
        return false; //All checks failed
    }

    public char winner() {
        if (this.currentPlayer == 'X') {
            return 'X';
        } else if (this.currentPlayer == 'O') {
            return 'O';
        }
        return ' ';
    }

    public String toString() {
        for (int i=0; i<7; i++) {
            for (int j=0; j<6; j++) {
                System.out.print(this.board[i][j]);
            }
            System.out.println();
        }
        return "";
    }
    public static void main(String[] args) {
        Board board = new Board();
        board.play(1);
        board.play(1);
        board.play(1);
        System.out.println(board);
    }
}
1qczuiv0

1qczuiv01#

写下这样的话:

throw new RuntimeException("the engines won't take it, Captain!");

你可能省略了“new”
或者拼写错误的“runtimeexception”(没有大写字母t)
我同意@maio290的观点,在这种情况下抛出illegalargumentexception更合适。illegalargumentexception是runtimeexception的一个子类,因此在这个意义上对调用方是“透明的”。

相关问题