【问题标题】:Interesting loops and booleans有趣的循环和布尔值
【发布时间】:2017-09-11 07:38:42
【问题描述】:

我正在为我的编程类进行编码,如果在 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 中,我们将不胜感激。

【问题讨论】:

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


    【解决方案1】:

    错误是您永远不会将连接设为假,只需添加最后一个 else 并使连接等于假或默认连接为假而不是真。

     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;
    

    【讨论】:

    • 所以我按照你说的做了,并创建了一个 else 语句,使 connection = false;并且原始方程也等于 false,但现在它所做的只是返回 false 作为答案。即使我将连接硬编码为等于 false。
    • 我的回答是做一个,要么默认为 false,要么添加 else 语句,但是我认为你应该设置条件 ((consecFour.length - Y
    • 答案是正确的 (+1) 。我添加了一个可能有帮助的解释。
    【解决方案2】:

    由于 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 吗?

    【讨论】:

      猜你喜欢
      • 2017-11-20
      • 2011-08-01
      • 2018-12-13
      • 1970-01-01
      • 2015-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多