有趣的循环和布尔

时间:2017-04-15 03:12:26

标签: java arrays multidimensional-array boolean nested-loops

我正在编写我的编程类,如果在2层多维数组中以对角线,垂直或水平方式连续有4个相等的值,那么它应该被认为是读取和精细的。这是我的代码。

public static boolean isConseccutiveFour (int[][] matrix){
    //creates a boolean to give answer later on
    boolean connection = true;
    int[][] consecFour = matrix;
    //incrementing for loops that move through the two arrays by going horizontally then diagonally
    for (int Y = 0; Y < consecFour.length - 3; Y++){
        for(int X= 0; X < consecFour[0].length - 3; X++){
            //if statement used to give the proper constraints for diagonal check 
            if ((consecFour.length - Y < 3) && (consecFour[0].length - X < 3)){
                if ( consecFour[X][Y] == consecFour[X + 1][Y + 1] && consecFour[X][Y] == consecFour[X+2][Y+2] && consecFour[X][Y] == consecFour[X+3][Y+3])
                    connection = true;
            }
            //if statement used to give the proper constraints for diagonal check 
            else if ((consecFour.length - Y < 3) && (consecFour[0].length < 3)){
                if ( consecFour[X][Y] == consecFour[X-1][Y-1] && consecFour[X][Y] == consecFour[X-2][Y-2] && consecFour[X][Y] == consecFour[X-3][Y-3])
                    connection = true;
            }
            //if statement used to give the proper constraints for horizontal check 
            else if (consecFour[0].length - X < 3){
                if(consecFour[X][Y] == consecFour[X+1][Y] && consecFour[X][Y] == consecFour[X+2][Y] && consecFour[X][Y] == consecFour[X+3][Y])
                    connection = true;
            }
            //if statement used to give the proper constraints for vertical check 
            else if (consecFour.length - Y < 3){
                if ( consecFour[X][Y] == consecFour[X][Y + 1] && consecFour[X][Y] == consecFour[X][Y+2] && consecFour[X][Y] == consecFour[X][Y+3])
                    connection = true;
            }
        }
    }
    //return statement of boolean value
    return connection;

我目前的问题是它总是返回true,无论放入什么阵列,我知道这可能看起来像一个愚蠢的错误,但我真的无法找到错误。我之前在我的主要语句中有这个方法被称为检查以确保输入数组的长度大于4且宽度大于4.这在java中已经知道并且答案将不胜感激。

2 个答案:

答案 0 :(得分:1)

错误是你永远不会连接到false,只需添加last else并使连接等于false或默认情况下使连接为false而不是true。

 public static boolean isConseccutiveFour (int[][] matrix){
        //creates a boolean to give answer later on
        boolean connection = false;
        int[][] consecFour = matrix;
        //incrementing for loops that move through the two arrays by going horizontally then diagonally
        for (int Y = 0; Y < consecFour.length - 3; Y++){
            for(int X= 0; X < consecFour[0].length - 3; X++){
                //if statement used to give the proper constraints for diagonal check 
                if ((consecFour.length - Y < 3) && (consecFour[0].length - X < 3)){
                    if ( consecFour[X][Y] == consecFour[X + 1][Y + 1] && consecFour[X][Y] == consecFour[X+2][Y+2] && consecFour[X][Y] == consecFour[X+3][Y+3])
                        connection = true;
                }
                //if statement used to give the proper constraints for diagonal check 
                else if ((consecFour.length - Y < 3) && (consecFour[0].length < 3)){
                    if ( consecFour[X][Y] == consecFour[X-1][Y-1] && consecFour[X][Y] == consecFour[X-2][Y-2] && consecFour[X][Y] == consecFour[X-3][Y-3])
                        connection = true;
                }
                //if statement used to give the proper constraints for horizontal check 
                else if (consecFour[0].length - X < 3){
                    if(consecFour[X][Y] == consecFour[X+1][Y] && consecFour[X][Y] == consecFour[X+2][Y] && consecFour[X][Y] == consecFour[X+3][Y])
                        connection = true;
                }
                //if statement used to give the proper constraints for vertical check 
                else if (consecFour.length - Y < 3){
                    if ( consecFour[X][Y] == consecFour[X][Y + 1] && consecFour[X][Y] == consecFour[X][Y+2] && consecFour[X][Y] == consecFour[X][Y+3])
                        connection = true;
                }
            }
        }
        //return statement of boolean value
        return connection;

答案 1 :(得分:0)

当user7790438正确回答时,connection永远不会设置为false,因此方法只能返回true。为了说明它,这是代码的框架:

public static boolean isConseccutiveFour (int[][] matrix){

        boolean connection = true;

        for (....){

                if (....){
                        ....
                        connection = true;
                }

                else if (....){
                        ....
                        connection = true;
                }
                else if (....){
                        ....
                        connection = true;
                }

                else if (....){
                        ....
                        connection = true;
                }       
        }

        return connection;
    }

你能在任何地方看到connection = false1吗?