mercredi 11 octobre 2017

Java- breaking out of an inner loop

I have a 2D Array and need to make a for loop that goes through each row and finds the index+1 when the integers stop increasing consecutively. For example if the first row is {1,2,3,4,9,10,11,20}, my method should set count1 = 4. The break statement is meant to terminate the inner loop and move on to the next sequence of the outer loop.

 public static int[][] reshuffle(int[][] board) {
        int count1 =0;
        int count2 =0;
        int count3 =0;
        int count4 =0;
        for(int i=0;i<4;i++) {
            for (int j = 0; j < 14; j++) {
                if (i==0 && board[i][j] + 1 != board[0][j + 1]) {
                    count1 = j+1;
                    break;
                } else if (i==1 && board[i][j] + 1 != board[1][j] + 1) {
                    count2 = j+1;
                    break;
                } else if (i==2 && board[i][j] + 1 != board[2][j] + 1) {
                    count3 = j+1;
                    break;
                } else if (i==3 && board[i][j] + 1 != board[3][j] + 1) {
                    count4 = j+1;
                    break;
                }

            }
        }
}

My program will return the correct value for count1, but always returns 0 for count2, count3, and count4. This indicates to me that the break statement is somehow terminating the outer loop as well as the inner.

Aucun commentaire:

Enregistrer un commentaire