简体   繁体   English

Java Sudoku-不更改2D数组中的字段

[英]Java Sudoku - Not Changing Fields In 2D Array

I've been running into an issue. 我一直遇到一个问题。 I'm relatively new at Java and am trying to bite off something a bit more complex than I'm used to. 我在Java方面相对较新,并且正在尝试简化一些比以前复杂的事情。 This is a combination of my own personal File input and main method with some cannibalized source for the other methods. 这是我自己的个人File输入和main方法与其他方法的某些替代源的结合。 I'm still fairly rusty with recursion. 我对递归仍然不满意。 For some reason, the assignment command to change values in the 2D Array "board" is running through without errors, but not changing the value. 由于某种原因,更改2D阵列“板”中的值的分配命令正常运行,但没有更改值。 Everything looks kosher to me in structure at least, but like I said, I'm new. 至少在结构上,一切看起来对我而言都是洁净的,但是就像我说的那样,我是新的。

Also, I'm looking for text output over terminal with the finished program, and throwing an exception seems to just be an eyesore over terminal. 另外,我正在寻找带有完成程序的终端上的文本输出,抛出异常似乎只是对终端的一种厌倦。 Any suggestions? 有什么建议么?

import java.util.Scanner;
import java.io.File;

public class Sudoku2{

    static int board[][] = new int[10][10] ;
    static int backtrack = 0;


    public static void main(String[] args) throws Exception {

        Sudoku2 myPuzzle = new Sudoku2();
        //  myPuzzle.readboard();
        myPuzzle.readData("./board/input.txt");
        myPuzzle.solve(0, 0);
        printboard();

    }
    protected static void printboard(){
        System.out.println("Here's your puzzle: ");
        for(int r = 0; r < 9; r++){
            for(int c = 0; c < 9; c++){
                System.out.print(board[r][c]+" ");
            }
            System.out.println("");
        }
    }

    public void readData(String filename) {
        File inputFile = new File(filename);
        try {
            Scanner keyboard = new Scanner(inputFile);
            for (int row = 0; row < 9; row++) {
                for (int col = 0; col < 9; col++) {

                    board[row][col] = keyboard.nextInt();
                }
            }
            keyboard.close();
        }catch(Exception e){
            System.out.print("Problem in readFile" + e);
            e.printStackTrace();
        }
    }

    //check if valid in row
    protected static boolean validInRow(int row, int value)
    {
        for( int col = 0; col < 9; col++ )
            if( board[row][col] == value )
                return false ;

        return true ;
    }

    //check if valid in column
    protected static boolean validInCol(int col, int value)
    {
        for( int row = 0; row < 9; row++ )
            if( board[row][col] == value )
                return false ;

        return true ;
    }

    //check if valid in 3*3
    protected static boolean validInBlock(int row, int col, int value)
    {
        row = (row / 3) * 3 ;
        col = (col / 3) * 3 ;

        for( int r = 0; r < 3; r++ )
            for( int c = 0; c < 3; c++ )
                if( board[row+r][col+c] == value )
                    return false ;

        return true ;
    }




    //call other methods
    public void solve(int row, int col) throws Exception
    {

        if(row > 8)
        {
            printboard();
            throw new Exception("Solution found") ;
        }
        else
        {

            while(board[row][col] != 0)
            {
                if( ++col > 8 )
                {
                    col = 0 ;
                    row++ ;


                    if( row > 8 )
                        printboard();
                    throw new Exception( "Solution found" ) ;
                }
            }


            for(int value = 1; value < 10; value++)
            {
                if(validInRow(row,value) && validInCol(col,value) && validInBlock(row,col,value))
                {

                    board[row][col] = value;
                    //new PrintEvent(board);



                    if( col < 8 )
                        solve(row, col + 1);
                    else
                        solve(row + 1, 0);

                    backtrack++;
                }
            }


            board[row][col] = 0;

        }
    }
}

Tenfour04's comment was correct. Tenfour04的评论是正确的。 There is a missing bracket in one of your if-statements. 您的一个if语句中缺少一个括号。 In your solve method, the following code: 在您的solve方法中,以下代码:

if ( row > 8 )
    printboard();
throw new Exception( "Solution found" ) ;

should be changed to: 应该更改为:

if ( row > 8 ) {
    printboard();
    throw new Exception( "Solution found" ) ;
}

In addition, as yourself mentioned, you are misusing Exception concept. 另外,正如您自己提到的,您正在滥用Exception概念。 Exception should be used for handling really exceptional, erroneous cases, not just for printing out something to the terminal. 应该将异常用于处理真正异常的错误情况,而不仅仅是将某些内容打印到终端上。

You can simply use the System.out.println method you used in the printboard method something like the following: 您可以简单地使用在printboard方法中使用的System.out.println方法,如下所示:

if ( row > 8 ) {
    printboard();
    System.out.println( "Solution found" ) ;
    return;
}

Here, I also added return keyword to make the program exit the solve method when a solution is found. 在这里,我还添加了return关键字,以使程序在找到solve方案时退出solve方法。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM