java - 抛出新的运行时异常问题
问题描述
任何人都可以帮助弄清楚如何实现抛出新的 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);
}
}
解决方案
写一些类似的东西:
throw new RuntimeException("the engines won't take it, Captain!");
您可能省略了“新”
或者可能拼错了“RuntimeException” (没有大写 T)
我同意@maio290 的观点,在这种情况下抛出 IllegalArgumentException 更为合适。IllegalArgumentException 是 RuntimeException 的子类,因此从这个意义上说对调用者来说是“透明的”。
推荐阅读
- c - 如何使堆栈可执行以进行教育堆栈粉碎?
- python - pySerial PermissionError(13,'访问被拒绝',无,5)
- python - subprocess.check_output 函数的行为问题
- sql - SQL Server 和 Oracle 兼容的多行插入?
- here-api - Geovisualization API 是否与 JavaScript 3.1 SDK 兼容?
- vbscript - VBS、fso.FolderExists如何正确设置路径
- python - 当我尝试安装 eel 时出现此错误
- html - 产品登陆页面的测试一直失败
- google-api - 超过/绕过 Google Calendar API 使用限制
- c++ - 奇怪的迭代器行为