Nolan
Nolan

Reputation: 75

Java, 2d array of boolean values && comparison

This is back to the beginning of Java 101, but I have this code here:

    if ((d==3)&&(City.walls[x--][y])){
        System.out.println ("Fourth Condition true");
        System.out.println (City.walls[x--][y]);
        return false;
    }

Even if City.walls[x--][y]) is false, and System.out.println confirms this by printing out false, it will still enter the if statement, no matter what. What am I doing wrong with the comparison? Thanks in advance.

Upvotes: 0

Views: 151

Answers (2)

Hardik Mishra
Hardik Mishra

Reputation: 14887

The values will be different when your code executes in if condition and then inside if condition where you are printing in console.

You are decrementing value of x in the if condition. So, When you print it inside condition you will get decremented value.

Upvotes: 2

Arcymag
Arcymag

Reputation: 1037

You are using x--, which changes the value of x. First it returns the value, then it decrements the value:

int x = 5;
System.out.println(x--); //outputs 5
System.out.println(x);   //outputs 4

You probably want to say x-1

Upvotes: 3

Related Questions