棋盘上的 8 个皇后
8 Queens on a Chessboard
我是初学者,我正在编写一个程序,将在棋盘上放置 8 个皇后。
我必须:
1) 创建一个空白棋盘(8X8 二维整数数组,全为 0)
2) 使用循环,向用户询问板上的 8 个位置,在此位置用 1 替换 0
3) 显示最终棋盘
我不必包括任何错误检查或重复位置检查。
我的代码没有正确存储值。
只有最后一个打印正确。
package chessboard;
import javax.swing.JOptionPane;
public class ChessBoard {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int chessBoardArray[][] = new int[8][8];
for (int z = 0; z < 8; z++) {
int rowValue = Integer.parseInt(JOptionPane.showInputDialog("Enter the row value (1-8)"));
int columnValue = Integer.parseInt(JOptionPane.showInputDialog("Enter the column value (1-8)"));
int rowValueFinal = rowValue - 1;
int columnValueFinal = columnValue - 1;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (i == rowValueFinal && j == columnValueFinal) {
chessBoardArray[i][j] = 1;
} else {
chessBoardArray[i][j] = 0;
}
}
}
}
for (int k = 0; k < 8; k++) {
if (k < 7) {
System.out.print(chessBoardArray[0][k]);
} else {
System.out.println(chessBoardArray[0][k]);
}
}
for (int k = 0; k < 8; k++) {
if (k < 7) {
System.out.print(chessBoardArray[1][k]);
} else {
System.out.println(chessBoardArray[1][k]);
}
}
for (int k = 0; k < 8; k++) {
if (k < 7) {
System.out.print(chessBoardArray[2][k]);
} else {
System.out.println(chessBoardArray[2][k]);
}
}
for (int k = 0; k < 8; k++) {
if (k < 7) {
System.out.print(chessBoardArray[3][k]);
} else {
System.out.println(chessBoardArray[3][k]);
}
}
for (int k = 0; k < 8; k++) {
if (k < 7) {
System.out.print(chessBoardArray[4][k]);
} else {
System.out.println(chessBoardArray[4][k]);
}
}
for (int k = 0; k < 8; k++) {
if (k < 7) {
System.out.print(chessBoardArray[5][k]);
} else {
System.out.println(chessBoardArray[5][k]);
}
}
for (int k = 0; k < 8; k++) {
if (k < 7) {
System.out.print(chessBoardArray[6][k]);
} else {
System.out.println(chessBoardArray[6][k]);
}
}
for (int k = 0; k < 8; k++) {
if (k < 7) {
System.out.print(chessBoardArray[7][k]);
} else {
System.out.println(chessBoardArray[7][k]);
}
}
}
}
这个循环不需要存在。
您正在清除整个面板并仅设置一个单元格
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (i == rowValueFinal && j == columnValueFinal) {
chessBoardArray[i][j] = 1;
} else {
chessBoardArray[i][j] = 0;
}
}
}
你应该用
替换整个循环
chessBoardArray[rowValue - 1][columnValue - 1] = 1;
我是初学者,我正在编写一个程序,将在棋盘上放置 8 个皇后。
我必须:
1) 创建一个空白棋盘(8X8 二维整数数组,全为 0)
2) 使用循环,向用户询问板上的 8 个位置,在此位置用 1 替换 0
3) 显示最终棋盘
我不必包括任何错误检查或重复位置检查。
我的代码没有正确存储值。
只有最后一个打印正确。
package chessboard;
import javax.swing.JOptionPane;
public class ChessBoard {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int chessBoardArray[][] = new int[8][8];
for (int z = 0; z < 8; z++) {
int rowValue = Integer.parseInt(JOptionPane.showInputDialog("Enter the row value (1-8)"));
int columnValue = Integer.parseInt(JOptionPane.showInputDialog("Enter the column value (1-8)"));
int rowValueFinal = rowValue - 1;
int columnValueFinal = columnValue - 1;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (i == rowValueFinal && j == columnValueFinal) {
chessBoardArray[i][j] = 1;
} else {
chessBoardArray[i][j] = 0;
}
}
}
}
for (int k = 0; k < 8; k++) {
if (k < 7) {
System.out.print(chessBoardArray[0][k]);
} else {
System.out.println(chessBoardArray[0][k]);
}
}
for (int k = 0; k < 8; k++) {
if (k < 7) {
System.out.print(chessBoardArray[1][k]);
} else {
System.out.println(chessBoardArray[1][k]);
}
}
for (int k = 0; k < 8; k++) {
if (k < 7) {
System.out.print(chessBoardArray[2][k]);
} else {
System.out.println(chessBoardArray[2][k]);
}
}
for (int k = 0; k < 8; k++) {
if (k < 7) {
System.out.print(chessBoardArray[3][k]);
} else {
System.out.println(chessBoardArray[3][k]);
}
}
for (int k = 0; k < 8; k++) {
if (k < 7) {
System.out.print(chessBoardArray[4][k]);
} else {
System.out.println(chessBoardArray[4][k]);
}
}
for (int k = 0; k < 8; k++) {
if (k < 7) {
System.out.print(chessBoardArray[5][k]);
} else {
System.out.println(chessBoardArray[5][k]);
}
}
for (int k = 0; k < 8; k++) {
if (k < 7) {
System.out.print(chessBoardArray[6][k]);
} else {
System.out.println(chessBoardArray[6][k]);
}
}
for (int k = 0; k < 8; k++) {
if (k < 7) {
System.out.print(chessBoardArray[7][k]);
} else {
System.out.println(chessBoardArray[7][k]);
}
}
}
}
这个循环不需要存在。
您正在清除整个面板并仅设置一个单元格
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (i == rowValueFinal && j == columnValueFinal) {
chessBoardArray[i][j] = 1;
} else {
chessBoardArray[i][j] = 0;
}
}
}
你应该用
替换整个循环chessBoardArray[rowValue - 1][columnValue - 1] = 1;