1

I have a for Loop, in which I want to increase the second index of a 2 dimensional Array and want to have a static first index:

String[][] grid = new String[7][6];

inject(0);

public void inject(int column){
    for(int i=5;i>-1;i--){
        if(grid[column][i] == null){
            if(player.equals("red")){
                grid[column][i] = "red";
                System.out.println("column: " + column + " i: " + i);
                break;
            }
            if(player.equals("yellow")){
                grid[column][i] = "yellow";
                System.out.println("column: " + column + " i: " + i);
                break;
            }
        }
    }
}

Expected Output of the console (if

grid[0][5]="yellow"; grid[0][4]="red";

):
column: 0 i: 5
column: 0 i: 4
column: 0 i: 3

Actual Output:
column: 0 i: 3
column: 1 i: 5
column: 2 i: 5
column: 3 i: 5
column: 4 i: 5
column: 5 i: 5
column: 6 i: 5

Why does column increase, I have no increment for column defined. I only want to check in one column what the last "null" array-field index is.

Calling of inject:

public void tasteReagieren(int key){
//Method of EngineAlpha to handle keyboard actions
    switch(key){
    case Taste._1: einwerfen(0);
    case Taste._2: einwerfen(1);
    case Taste._3: einwerfen(2);
    case Taste._4: einwerfen(3);
    case Taste._5: einwerfen(4);
    case Taste._6: einwerfen(5);
    case Taste._7: einwerfen(6);
    }
}
Ichor de Dionysos
  • 1,107
  • 1
  • 8
  • 30

1 Answers1

0

You have to implement break; after each case:

Ichor de Dionysos
  • 1,107
  • 1
  • 8
  • 30