Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java int equality inconsistency? [closed]

This is driving me insane because it completely violates my attempts to de-buggify it:

int k = keyCode; //keyCode being a variable declared by a keyPress
//in the Processing library
//k and keyCode are working properly.
if ((k - UP)*500 == 0); //int UP=38;
{
 println((k-UP)*500 == 0);
 //some other code here
}

The result? "false" (and by removing the '== 0', a number that isn't 0). As far as I know only when you use the arrow keys (k==37,38,39,40; 38 being UP) make this condition true.

Is there any such inconsistency, and what may cause it? (The strange format of the condition is because it fixed a similar problem with the RIGHT key not working correctly with just k==RIGHT).

like image 320
BlueShamen Avatar asked Jan 21 '26 01:01

BlueShamen


2 Answers

You have a semicolon after if, so println is always executed.

like image 95
quant_dev Avatar answered Jan 23 '26 16:01

quant_dev


You had a ; after your if. That makes it an if with an empty statment, followed by an unconditiend block. Just remove the semicolon in the if line.

like image 43
flolo Avatar answered Jan 23 '26 15:01

flolo