gpt4 book ai didi

java - 我有一个二维字符数组,由于某种原因,它不允许我将数组内的字符与另一个字符进行比较?

转载 作者:行者123 更新时间:2023-12-01 12:13:37 25 4
gpt4 key购买 nike

public class Driver {

public static void main(String[] args) {

Driver run = new Driver();
run.it();

}

public void it() {

Tester test = new Tester();
test.fillBoard();
test.placeQueens();
test.printBoard();

}

}

public class Tester {

public char [][] board = new char [8][8];

public void fillBoard() {

for(int i = 0; i < 8; i++) {

for(int j = 0; j < 8; j++) {

board[i][j] =' ';

}

}

}

public boolean checkUp(int row , int col) {

if (((row < 0) || (row > 7)) || ((col > 7) || (col < 0))) {

if (board[row][col] == 'Q') {

return false;

} else {

return checkUp(row - 1, col);

}

} else {

return true;

}

}

public boolean checkDown(int row , int col) {

if (((row < 0) || (row > 7)) || ((col > 7) || (col < 0))) {

if (board[row][col] == 'Q') {

return false;

} else {

return checkDown(row + 1, col);

}

} else {

return true;

}

}

public boolean checkUpAndRight(int row , int col) {

if (((row < 0) || (row > 7)) || ((col > 7) || (col < 0))) {

if (board[row][col] == 'Q') {

return false;

} else {

return checkUpAndRight(row - 1, col - 1);

}

} else {

return true;

}

}

public boolean checkDownAndRight(int row , int col) {

if (((row < 0) || (row > 7)) || ((col < 0) || (col > 7))) {

if (board[row][col] == 'Q') {

return false;

} else {

return checkDownAndRight(row + 1, col - 1);

}

} else {

return true;

}

}

public boolean checkUpAndLeft(int row, int col) {

if (((row < 0) || (row > 7)) || ((col > 7) || (col < 0))) {

if (board[row][col] == 'Q') {

return false;

} else {

return checkUpAndLeft(row - 1, col + 1);

}

} else {

return true;

}

}

public boolean checkDownAndLeft(int row , int col) {

if (((row < 0) || (row > 7)) || ((col > 7) || (col < 0))) {

if (board[row][col] == 'Q') {

return false;

} else {

return checkDownAndLeft(row + 1, col + 1);

}

} else {

return true;

}

}

public boolean checkLeft(int row , int col) {

if (((row < 0) || (row > 7)) || ((col > 7) || (col < 0))) {

if (board[row][col] == 'Q') {

return false;

} else {

return checkLeft(row, col + 1);

}

} else {

return true;

}

}

public boolean checkRight(int row , int col) {

if ((col > 7) || (col < 0)) {

if (board[row][col] == 'Q') {

return false;

} else {

return checkRight(row, col - 1);

}

} else {

return true;

}

}

public boolean checkSpot(int row, int col) {

if ((checkUp(row, col) == true) && (checkDown(row, col) == true) &&
(checkUpAndRight(row, col) == true) && (checkDownAndRight(row, col) == true) &&
(checkUpAndLeft(row, col) == true) && (checkDownAndLeft(row, col) == true) &&
(checkLeft(row, col) == true) && (checkRight(row, col) == true)) {

return true;

} else {

return false;

}

}


public void placeQueens() {


for(int col = 0; col < 8; col++) {

for(int row = 0; row < 8; row++) {

if(checkSpot(row, col) == true) {

board[row][col] = 'Q';

}

}

}

}

public void printBoard() {

String rtn = "";
String newRow = "+---+---+---+---+---+---+---+---+\n";
rtn += newRow;
rtn += "| "+ board[0][0] +" | "+ board[0][1] +" | "+ board[0][2] +" | "+ board[0][3] +" | "
+ board[0][4] +" | "+ board[0][5] +" | "+ board[0][6] +" | "+ board[0][7] +" |\n";
rtn += newRow;
rtn += "| "+ board[1][0] +" | "+ board[1][1] +" | "+ board[1][2] +" | "+ board[1][3] +" | "
+ board[1][4] +" | "+ board[1][5] +" | "+ board[1][6] +" | "+ board[1][7] +" |\n";
rtn += newRow;
rtn += "| "+ board[2][0] +" | "+ board[2][1] +" | "+ board[2][2] +" | "+ board[2][3] +" | "
+ board[2][4] +" | "+ board[2][5] +" | "+ board[2][6] +" | "+ board[2][7] +" |\n";
rtn += newRow;
rtn += "| "+ board[3][0] +" | "+ board[3][1] +" | "+ board[3][2] +" | "+ board[3][3] +" | "
+ board[3][4] +" | "+ board[3][5] +" | "+ board[3][6] +" | "+ board[3][7] +" |\n";
rtn += newRow;
rtn += "| "+ board[4][0] +" | "+ board[4][1] +" | "+ board[4][2] +" | "+ board[4][3] +" | "
+ board[4][4] +" | "+ board[4][5] +" | "+ board[4][6] +" | "+ board[4][7] +" |\n";
rtn += newRow;
rtn += "| "+ board[5][0] +" | "+ board[5][1] +" | "+ board[5][2] +" | "+ board[5][3] +" | "
+ board[5][4] +" | "+ board[5][5] +" | "+ board[5][6] +" | "+ board[5][7] +" |\n";
rtn += newRow;
rtn += "| "+ board[6][0] +" | "+ board[6][1] +" | "+ board[6][2] +" | "+ board[6][3] +" | "
+ board[6][4] +" | "+ board[6][5] +" | "+ board[6][6] +" | "+ board[6][7] +" |\n";
rtn += newRow;
rtn += "| "+ board[7][0] +" | "+ board[7][1] +" | "+ board[7][2] +" | "+ board[7][3] +" | "
+ board[7][4] +" | "+ board[7][5] +" | "+ board[7][6] +" | "+ board[7][7] +" |\n";
rtn += newRow;
System.out.print(rtn);
}
}

So many of my methods search through the array looking for the char 'Q' using this if statement

if (board[row][col] == 'Q') {

return false;

} else {

return checkRight(row, col - 1);

}

但是我的输出总是

+---+---+---+---+---+---+---+---+

|问 |问 |问 |问 |问 |问 |问 |问 |

+---+---+---+---+---+---+---+---+

|问 |问 |问 |问 |问 |问 |问 |问 |

+---+---+---+---+---+---+---+---+

|问 |问 |问 |问 |问 |问 |问 |问 |

+---+---+---+---+---+---+---+---+

|问 |问 |问 |问 |问 |问 |问 |问 |

+---+---+---+---+---+---+---+---+

|问 |问 |问 |问 |问 |问 |问 |问 |

+---+---+---+---+---+---+---+---+

|问 |问 |问 |问 |问 |问 |问 |问 |

+---+---+---+---+---+---+---+---+

|问 |问 |问 |问 |问 |问 |问 |问 |

+---+---+---+---+---+---+---+---+

|问 |问 |问 |问 |问 |问 |问 |问 |

+---+---+---+---+---+---+---+---+

让我想知道为什么这个陈述总是回答错误?

board[row][col] == 'Q'

最佳答案

每个方法都以此行开头:

if (((row < 0) || (row > 7)) || ((col > 7) || (col < 0))) {

然后继续检查由 rowcol 给出的位置。但您已经检查过您是在董事会之外,还是在董事会内。

切换您的 ifelse block ,这样,如果您离开棋盘,您会立即返回 true,否则您就在棋盘上板,您可以检查'Q'的位置。

关于java - 我有一个二维字符数组,由于某种原因,它不允许我将数组内的字符与另一个字符进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27134247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com