首页 > 解决方案 > 抛出新的运行时异常问题

问题描述

任何人都可以帮助弄清楚如何实现抛出新的 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);
    }
}

标签: javaoop

解决方案


写一些类似的东西:

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

您可能省略了“新”

或者可能拼错了“RuntimeException” (没有大写 T)


我同意@maio290 的观点,在这种情况下抛出 IllegalArgumentException 更为合适。IllegalArgumentException 是 RuntimeException 的子类,因此从这个意义上说对调用者来说是“透明的”。


推荐阅读