private int getNextNodesDirection(Pair<Integer, Integer> current, Pair<Integer, Integer> nextNode, Robot target) {
System.out.println("current x:" + current.getSecond());
System.out.println("current y:" + current.getFirst());
System.out.println("next x:" + nextNode.getSecond());
System.out.println("next y:" + nextNode.getFirst());
if (target.getCurrentDirection().equals(IWorld.Direction.UP)) {
//if y increases and x stays the same
if (nextNode.getFirst() > current.getFirst() && nextNode.getSecond().equals(current.getFirst())) {
return 0;
}
// if x increases and y stays the same
else if (nextNode.getSecond() > current.getSecond() && nextNode.getFirst().equals( current.getFirst())) {
return 1;
}
while (index < endIndex){
int fSteps = 0;
int direction = getNextNodesDirection(nodeList.get(index), nodeList.get(index+1), target);
if (direction == 1){
cost+=1;
target.handleCommand(new RightCommand());
}
else if (direction == 2){
cost+=2;
target.handleCommand(new RightCommand());
target.handleCommand(new RightCommand());
}
else if (direction == 3){
cost+=1;
target.handleCommand(new LeftCommand());
}
while (direction == 0){
fSteps += 1;
cost+=fSteps;
index+=1;
if(nodeList.get(index).getSecond() == (end.getX()) && nodeList.get(index).getFirst() == end.getY()){
break;
}
direction = getNextNodesDirection(nodeList.get(index), nodeList.get(index+1), target);
因此,当程序运行时,它进入 getNextNodesDirection 并按预期执行,但在第二轮时,尽管满足了所有条件,但它只是通过了语句。为什么?我希望它在 nextNode.getFirst()
大于 current.getFirst
且 nextNode.getSecond()
等于 current.getSecond()
我在 IntelliJ 中截取了 here 的截图,在调试时,您可以清楚地看到 y 正在增加而 x 保持不变,但它通过了“满足”条件并继续进行。为什么?
语言是 Java 11,操作系统是虚拟机上的 Debian。