gpt4 book ai didi

java - 点 x 值无故变化

转载 作者:行者123 更新时间:2023-11-29 04:48:38 24 4
gpt4 key购买 nike

我有这种方法可以在矩阵中寻找空闲位置。这是我打印 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,但这总是返回 false,因为我在里面设置了 stop = true第一个 for 循环这是我运行此方法时得到的输出:

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+")");
}

关于java - 点 x 值无故变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36284438/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com