点 x 值无故变化

Point x value changes for no reason

我有这种方法可以在矩阵中寻找空闲位置。这是我打印 matrix 时得到的输出。

1 0 0 0 0 0 0 
1 1 1 0 0 0 0 
1 1 1 0 0 0 0 
0 1 0 0 0 0 0 
0 0 0 0 0 0 0 
0 0 0 0 0 0 0 
0 0 0 0 0 0 0

我有 currentPos(0,0)。下一个空闲位置应该是 (0,1)。当我调用 NextFreePos() 时,它会更改 currentPos.x 值,即使它不应该更改。如果我在第二个 for 循环中得到 true,我只会更改 x,但这总是 returns false,因为我在第一个 for 循环中创建 stop = true 这是我 运行 此方法时得到的输出:

NEXT POS = (0;0)
SAME X
NEXT POS = (1;1) // I dont get why the x (first value) changed to 1

 public void nextFreePos(Point currentPos){
        // display the OLD POS (x,y) <-- x = row and y = column
        System.out.println("OLD POS = ("+(currentPos.x)+";"+currentPos.y+")");       
        boolean stop = false;
        // check if there is a free Pos in current row, starting from current y
        for(int y = currentPos.y; y < 7; y++){
            // check if Position is free and no free position has been found yet
            if(matrix[currentPos.x][y] == 0 && stop == false){
                stop = true;
                System.out.println("SAME X");
                currentPos.y = y;
            }
        }        
        currentPos.x++;        
        for(int x = currentPos.x; x < 7; x++){
            for(int y = 0; y < 7; y++){
                // check if Position is free and no free position has been found yet
                if(matrix[x][y] == 0 && stop == false){
                    stop = true;
                    System.out.println("OTHER X");
                    // set the position to x and y
                    currentPos.x = x;
                    currentPos.y = y;  
                }                
            }
        }  
        // display the NEXT POS (x,y)
        System.out.println("NEXT POS = ("+currentPos.x+";"+currentPos.y+")");       
    }

查看突出显示的代码段。

NEXT POS = (0;0)
SAME X
NEXT POS = (1;1) // I dont get why the x (first value) changed to 1

 public void nextFreePos(Point currentPos){
        // display the OLD POS (x,y) <-- x = row and y = column
        System.out.println("OLD POS = ("+(currentPos.x)+";"+currentPos.y+")");       
        boolean stop = false;
        // check if there is a free Pos in current row, starting from current y
        for(int y = currentPos.y; y < 7; y++){
            // check if Position is free and no free position has been found yet
            if(matrix[currentPos.x][y] == 0 && stop == false){
                stop = true;
                System.out.println("SAME X");
                currentPos.y = y;
            }
        }        
        currentPos.x++;
        for(int x = currentPos.x; x < 7; x++){
            for(int y = 0; y < 7; y++){
                // check if Position is free and no free position has been found yet
                if(matrix[x][y] == 0 && stop == false){
                    stop = true;
                    System.out.println("OTHER X");
                    // set the position to x and y
                    currentPos.x = x;
                    currentPos.y = y;  
                }                
            }
        }  
        // display the NEXT POS (x,y)
        System.out.println("NEXT POS = ("+currentPos.x+";"+currentPos.y+")");       
    }

您在 (0,1) 处找到一个开仓并设置 stop = true;,然后在第一个 for 循环之后调用 currentPos.x++;这会增加 x。下一个循环什么都不做,因为 stop 已经是 false。