【问题标题】:Java Sudoku - Not Changing Fields In 2D ArrayJava Sudoku - 不更改二维数组中的字段
【发布时间】:2013-10-06 21:26:08
【问题描述】:

我遇到了一个问题。我在 Java 方面相对较新,并且正试图咬掉一些比我以前更复杂的东西。这是我自己的个人文件输入和主要方法与其他方法的一些蚕食源的组合。我对递归仍然相当生疏。出于某种原因,用于更改 2D 阵列“板”中值的分配命令正在运行而没有错误,但没有更改值。至少在结构上,一切看起来对我来说都是合理的,但就像我说的,我是新人。

另外,我正在使用已完成的程序在终端上寻找文本输出,而抛出异常似乎只是终端上的一个麻烦事。有什么建议吗?

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;

        }
    }
}

【问题讨论】:

  • 您应该正确格式化您的代码,这很难阅读。你的 IDE 应该有一个自动格式化选项。
  • 我马上注意到的一件事是你没有把你的 if 语句括起来,但是你在其中一些之后缩进了多个调用,这让我想也许你认为这会使它们成为 if 的一部分陈述。如果没有括号,则 if 语句仅包含其后的单行。
  • 抛出异常作为输出是根本错误的。给solve() 方法一个返回类型或让它修改板,然后调用printBoard()。无论如何,您已经在打印到 System.out,所以我不明白问题的那一部分。取而代之的是控制语句,如return;break;

标签: java arrays recursion sudoku


【解决方案1】:

Tenfour04 的评论是正确的。您的一个 if 语句中缺少一个括号。 在您的solve 方法中,以下代码:

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

应该改为:

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

此外,正如您自己所提到的,您在滥用异常概念。 Exception 应该用于处理真正异常、错误的情况,而不仅仅是将某些内容打印到终端。

您可以简单地使用您在printboard 方法中使用的System.out.println 方法,如下所示:

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

这里我还添加了return关键字,让程序在找到解决方案时退出solve方法。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2015-09-12
    • 2018-06-25
    • 1970-01-01
    • 2017-04-16
    • 2012-02-24
    • 2015-03-22
    • 1970-01-01
    • 2017-03-16
    • 1970-01-01
    相关资源
    最近更新 更多