Quantcast
Viewing latest article 2
Browse Latest Browse All 4

Answer by marstran for Java not reading "&&" statements properly?

Using the modulo operator with negative numbers gives you a different result than you might think.

1 % 2 == 12 % 2 == 0-2 % 2 == 0-1 % 2 == -1

To get the result you want, you can replace your modulo tests with num % 2 == 0 and num % 2 != 0.


Viewing latest article 2
Browse Latest Browse All 4

Trending Articles