2017-04-15 9 views
1

Ich bin für meine Programmierklasse, die zum Lesen gemacht werden soll, und fein, wenn in einem mehrschichtigen 2-Schicht-Array 4 gleiche Werte in einer Reihe in einer diagonalen, vertikalen oder horizontalen Art. Hier ist mein Code.Interessante Schleifen und Booleans

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; 

Mein aktuelles Problem ist, dass es immer true zurück, egal was passiert gestellt hat in ist und ich weiß, das ist wie ein dummer Fehler erscheinen, aber ich kann wirklich nicht finden, was falsch ist. Ich habe in meiner Hauptaussage, bevor diese Methode eine Überprüfung aufgerufen wird, um sicherzustellen, dass das Eingabearray eine Länge größer als 4 und die Breite größer als 4 hat. Dies ist in Java, wie Sie bereits wissen und Antworten würden geschätzt.

Antwort

1

Der Fehler ist, dass Sie niemals eine Verbindung zu false herstellen, fügen Sie einfach last else hinzu und machen Sie die Verbindung gleich falsch oder stellen Sie sicher, dass die Verbindung falsch ist.

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

Also habe ich getan, was Sie sagten und eine else-Anweisung erstellt, die connection = false; und auch die ursprüngliche Gleichung ist gleich falsch, aber jetzt ist alles, was sie tut, falsch als Antwort. selbst wenn ich eine Verbindung fest codiert habe um gleich falsch zu sein. – JamesJSchindler

+0

meine antwort war es entweder standardmäßig false zu machen oder eine else statement hinzuzufügen, aber ich denke du solltest die bedingung machen ((consecFour.length - Y <3) && (consecFour [0] .length - X <3)) zu sein ((FolgeLänge - 1) - Y <= 3) && ((Folge [0] .Länge -1) - X <= 3) Da Länge ab 1 und Schleife ab 0 –

+0

Die Antwort ist richtig (+1). Ich fügte eine Erklärung hinzu, die helfen könnte. – c0der

0

Wie user7790438 richtig beantwortet, wird connection nie auf false gesetzt, so Methode kann nur true zurück. Zur Veranschaulichung es, hier ist das Skelett des Codes:

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; 
    } 

Können Sie sehen connection = false1 überall?