【发布时间】:2014-06-03 12:52:00
【问题描述】:
我的数独求解器工作得很好,现在我要做的就是 当用户单击“解决”按钮时“检查”。这是一个采用自定义数独的用户输入, 因此,根据用户在框上的输入,求解器将求解。
我正在尝试检查用户是否输入了无效值,例如“1 1 2 3 4 5 6 7 8”,因为“1 1”而这将是错误的。因为我有自己的 checkRow、checkBox、checkCol 方法,用于解决难题,所以我会在运行之前使用这些方法检查任何无效值 - 因为如果用户输入无效值,程序将冻结。
非常奇怪的是,我的 sysout 插入尝试调试,我得到以下信息:
I, J, NUM 0 0 1
方法行:真
主R:假
主 C: 假
主B:假
方法行:真
检查:0、0、假
方法行:真
which METHOD 显示 checkRow 方法的返回值,而 Main R 显示从解决 actionListener 调用时的返回值,位于 main() 中。
线程问题??我对 JAVA 不太熟悉,所以我确实将大部分代码放在了 main 中(我知道这不好,但我仍在学习)。
提前感谢您的帮助。
solve.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
solve.setEnabled(false);
for (int i=0; i<9; i++) {
for (int j=0; j<9; j++) {
try {
problem[i][j]=Integer.parseInt(board[i][j].getText());
System.out.println("I, J, NUM " +i+" "+j+" "+problem[i][j]);
System.out.println("TOP R: " + checkRow(i, problem[i][j]));
System.out.println("TOP C: " + checkCol(j, problem[i][j]));
System.out.println(("TOP B:" + checkBox(i, j, problem[i][j])));
if(problem[i][j] != 0){
System.out.println
("Check: " + i +", "+j +", " +check(i, j, problem[i][j]));
if( !check(i, j, problem[i][j])) {
solve.setEnabled(true);
JOptionPane.showMessageDialog(null, "Bad Input!");
clear_board();
return;
}
}
}catch(NumberFormatException e) {
problem[i][j] = 0;
}
}
}
try {
solve_s(0,0);
} catch (Exception e) {
}
}
});
/** 检查 num 是否是给定行的可接受值 */
protected static boolean checkRow( int row, int num )
{
for( int col = 0; col < 9; col++ ) {
if( problem[row][col] == num ) {
System.out.println("ROW C: "+ (problem[row][col] == num));
return false ;
}
return true;
}
/** Checks if num is an acceptable value for the given column */
protected static boolean checkCol( int col, int num )
{
for( int row = 0; row < 9; row++ ){
// System.out.println("COL C: "+ (problem[row][col] == num));
if( problem[row][col] == num )
return false ;
}
return true ;
}
/** Checks if num is an acceptable value for the box around row and col */
protected static boolean checkBox( int row, int col, int num )
{
row = (row / 3) * 3 ;
col = (col / 3) * 3 ;
for( int r = 0; r < 3; r++ ) {
for( int c = 0; c < 3; c++ ) {
if( problem[row+r][col+c] == num ) {
// System.out.println("BOX C: "+ (problem[row][col] == num));
return false ;
}
}
}
return true ;
}
【问题讨论】: