2017-04-15 75 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地方?