如何在Java中的数独检查器中从布尔值切换到int?

时间:2017-12-02 19:53:08

标签: java multidimensional-array

我正在创建一个sodoku检查器。这是我到目前为止所做的,但我有点碰壁。在我的检查器中,我不允许将方法中的一个从int更改为布尔值,这对我来说很难。

public class SudokuVerifier {
public static int[][] candidateSolution()
{
    Scanner input = new Scanner(System.in);
    int [][] sudokuboard = new int [9][9];

    for (int i = 0; i < 9; i++)
        for (int j = 0; j < 9; j++)
            sudokuboard[i][j] = input.nextInt();

    input.close();
    return sudokuboard;
}
public int verify(String candidateSolution)
//checking rows
{
    for (int i = 0; i < sudokuboard.length; i++ )
        if (validcheck(sudokuboard[i]))
            return 1;

}
/**
 * Tells the candidate to enter their sodoku puzzle
 * it then gets stored into a 2 dimensional array
 * array gets returned
 * tells them if it's right or wrong
 * @author sultan
 *
 */
public static void main(String[] args)
{
 int [][] sudokuboard = candidateSolution();
 System.out.print(solutionChecker(sudokuboard) ? "right solution" : "Wrong solution");

}
public static boolean solutionChecker(int [][] sudokuboard)
{

return true;
}

}

&#34; public int verify&#34;是我无法改变的部分,它给了我错误。所以我想知道是否有一种方法可以保持这些代码大致相同,但使用ints代替我的返回而不是布尔来计算行,列和3X3板。

2 个答案:

答案 0 :(得分:0)

返回int 1表示正确,0表示不正确。假设如果组合正确,则validcheck返回true:

public int verify(String candidateSolution)
//checking rows
{
    for (int i = 0; i < sudokuboard.length; i++ )
        if (! validcheck(sudokuboard[i]))
            return 0;

    return 1;

}

有些语言(如ANSI C)根本不使用布尔值。 0表示未满足条件,0以外的任何条件表示条件已满足。

答案 1 :(得分:0)

当if-Statement为true时,您的函数只返回一个值。 当语句为false时,您需要添加以返回int。

Moritz的