简体   繁体   English

如何将方法从布尔值转换为int(java)?

[英]How to convert a method from boolean to int (java)?

so I have to build a Sudoku game in Java. 所以我必须用Java构建Sudoku游戏。 I am able confirm in my method that the numbers 1 to 9 only appear once in each row/column. 我可以用我的方法确认数字1到9在每一行/列中只出现一次。 However, I have this set up as a boolean and can not for the life of me figure out how to convert this to an integer (so that I can return the row/column where the error occurs). 但是,我将此设置为布尔值,并且无法终生弄清楚如何将其转换为整数(以便我可以返回发生错误的行/列)。

public static boolean rowColumnCheck(int[][] array) {

    for (int i = 0; i < 9; i++) {
        boolean[] rowArray = new boolean[9];
        boolean[] columnArray = new boolean[9];
        for (int j = 0; j < 9; j++) {
            int currentNumberRow = array[i][j];
            int currentNumberColumn = array[j][i];
            if ((currentNumberRow < 1 || currentNumberRow > 9)
                    && (currentNumberColumn < 1 || currentNumberColumn > 9)) {
                return false;
            }
            rowArray[currentNumberRow - 1] = true;
            columnArray[currentNumberColumn - 1] = true;

        }
        for (boolean booleanValue : rowArray) {
            if (!booleanValue) {
                return false;
            }
        }
        for (boolean booleanValue : columnArray) {
            if (!booleanValue) {
                return false;
            }
        }
    }
    return true;
}

You can't. 你不能 Each method basically has just a return type and a boolean is not compatible with an Integer. 每个方法基本上只有一个返回类型,并且布尔值与Integer不兼容。 What you could do is change the return type to Integer or to Pair if you need to return a set of coordinates and return null if is not there. 如果需要返回一组坐标,则可以将返回类型更改为Integer或Pair,如果不存在则返回null。

I guess it would be something like this. 我猜可能是这样的。 If null there was no error, and on the pair is the error location. 如果为null,则没有错误,并且在错误对上是错误位置。

public static Pair rowColumnCheck(int[][] array) {

    Pair <Integer, Integer> p = null;

    for (int i = 0; i < 9; i++) {
        boolean[] rowArray = new boolean[9];
        boolean[] columnArray = new boolean[9];
        for (int j = 0; j < 9; j++) {
            int currentNumberRow = array[i][j];
            int currentNumberColumn = array[j][i];
            if ((currentNumberRow < 1 || currentNumberRow > 9)
                    && (currentNumberColumn < 1 || currentNumberColumn > 9)) {
                p = new Pair<Integer, Integer>(i, j);
                return p;
            }
            rowArray[currentNumberRow - 1] = true;
            columnArray[currentNumberColumn - 1] = true;

        }
        // Not really sure why you are doing this?
        for (boolean booleanValue : rowArray) {
            if (!booleanValue) {
                return null;
            }
        }
        for (boolean booleanValue : columnArray) {
            if (!booleanValue) {
                return null;
            }
        }
    }
    return null;
}

I have a feeling you want sometimes to return a row/column pair, sometimes a row, and sometimes a column. 我有一种感觉,您希望有时返回行/列对,有时返回行,有时返回列。 If this is right, then you need to create an extra class: 如果正确,那么您需要创建一个额外的类:

public class RowCol {
    public final int row;
    public final int col;

    public RowCol(int row, int col) {
        this.row = row;
        this.col = col;
    }
}

Now when you want to identify the place where the error occurred, you can 现在,当您要确定错误发生的位置时,您可以

return new RowCol(i,j);

or to indicate a row without specifying a column 或表示未指定列的行

return new RowCol(i,-1);

and similarly for a column 和类似的列

return new RowCol(-1,j);

The return type of your method will be RowCol , and when you get one back from your method, you'll be able to interrogate its row and col fields to find out the co-ordinates of the return value. 方法的返回类型将为RowCol ,当您从方法中获得返回值时,您可以查询其rowcol字段以找出返回值的坐标。

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

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