首页 文章

在向参考数组添加元素之后,java继续检查构造函数中的原始数组

提问于
浏览
0

我使用2D数组来创建一个sudokuPuzzle游戏,但是在用户输入一个值之后,编译器会继续检查构造函数中的原始数组!

public void addInitial(int row, int column, int value) {

    for (int i = 0; i < board.length; i++) {

        if (board[i][column] == value || board[row][i] == value || board[row][column] != 0) {
            //first condition to check the place itself. Second condition to check the rows, third for columns. fourth to check if the given row and column is a blank space (0)
            //not solved correctly yet
            System.out.println("Wrong value, try again!");
            break;

        }
        if (board[i][column] != value || board[row][i] != value || board[row][column] == 0) {
            board[row][column] = value;
            System.out.println(value + " has been added");

            break;
        }
    }
}


public class Sudoku {
    public static void main (String[] args) {
        Scanner input = new Scanner(System.in);

        SudokuPuzzle game = new SudokuPuzzle();
        int i = 0;
        do{
            System.out.println(game);
            i++;
            System.out.println("Enter row, column, and value");
            System.out.print("Row: ");
            int r = input.nextInt() - 1;
            System.out.print("Column: ");
            int c = input.nextInt() - 1;
            System.out.print("Value: ");
            int v = input.nextInt();
            game.addInitial(r, c, v);

        }while(i < 3); // the loop is just to try 3 values
    }
}

我不希望用户在彼此之上输入两个相同的值或彼此相邻,但编译器允许这样做,因为它继续检查构造函数中的原始数组 .

1 回答

  • 0

    我对你对'原始数组'的意思感到有些困惑,因为你还没有提供解释,但似乎你遇到的问题是你的程序正在分析你不喜欢的板/阵列我想要检查 . 无论如何,根据您提供的代码,我将采取措施 .

    我认为您的问题可能源于这样一个事实,即您似乎正在编辑您正在引用的同一个板,以确定答案是否正确 . 我建议你使用两个不同的板:一个用所有正确的答案开始填充(并且对玩家隐藏),然后是第二个板,将由玩家在游戏过程中填写 . 因此,在您的实现中,您将获取玩家在main中给出的值,然后检查已填写的“回答”板,然后如果玩家的猜测与棋盘上的值匹配,则您将该值输入到玩家的棋盘中 .

    如果这不是你真正的问题,我会道歉并删除我的答案,但由于你没有提供太多的背景,我以为我会采取行动 . 希望这可以帮助 .

相关问题