【发布时间】:2018-12-08 20:34:18
【问题描述】:
我用二维数组创建了一个数独游戏,但是用户输入一个值后,编译器一直在构造函数中检查原始数组!
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
}
}
我不希望用户在彼此上方或彼此相邻的地方输入两个相同的值,但编译器允许这样做,因为它会不断检查构造函数中的原始数组。
【问题讨论】:
-
"compiler keep checking on the original array in the constructor!"这是什么意思?什么原创Array? -
不确定你对原始数组的意思——但这行不会检查你是否已经在 s roe 或列中添加了一个值——最好把它写在纸上
if (board[i][column] != value || board[row][i] != value || board[row][column] == 0) { -
我的意思是构造函数中的数组。我在构造函数中初始化了数组。