Quantcast
Channel: Java not reading "&&" statements properly? - Stack Overflow
Viewing all articles
Browse latest Browse all 4

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

$
0
0

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 all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>