Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        2年前关闭。
                                                                                            
                
        
我是一个初学者,正在创建一个程序,将8个皇后放在棋盘上。
我必须:

1)创建一个空白棋盘(8X8 2D整数数组,全为0)
2)使用循环,要求用户在板上放置8个位置,在该位置将0替换为1
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;

10-01 14:14