2017-04-15 57 views
1

我为我的编程类编码,如果在一个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中,因为你已经知道和答案将不胜感激。

回答

1

错误是你从来没有连接到false,只需添加最后一个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; 
+0

所以我做了你所说的,并创建了一个else语句,使connection = false;并且原始方程式也是相同的,但现在所做的只是返回错误的答案。即使当我硬编码一个连接等于false。 – JamesJSchindler

+0

我的答案是要做一个默认为false或添加一个else语句,但我认为你应该使条件((consecFour.length - Y <3)&&(consecFour [0] .length - X <3) )是((consecFour.length - 1) - Y <= 3)&&((consecFour [0] .length -1) - X <= 3) 因为长度从1开始,循环从0开始 –

+0

答案是正确(+1)。我添加了一个可能有所帮助的解释。 – c0der

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地方?