jeudi 1 avril 2021

Why is this if statement not fufilled when all the conditions are met?

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);

So when the program runs it enters into getNextNodesDirection and executes as expected but on the second time round, although all the conditions are fulfilled it just goes passed the statement. Why? I would like it to return 0 while nextNode.getFirst() is greater than current.getFirst and nextNode.getSecond() is equal to current.getSecond()

I've screenshotted here in IntelliJ, while debugging, you can clearly see that y is increasing and x staying the same yet it passes the "fulfilled" condition and carries on. Why?

The language is Java 11, OS is Debian on a virtual machine.

Aucun commentaire:

Enregistrer un commentaire