debug this or System.out.println the variable values before and after your assignment. You'll see that the original value was not 5, or the code is being executed more than one time, what makes it negative after a few iterations.

. I hope you know that if you put that function in the "if" statement, it will perform the function every loop which will change your values if the check passes or not. Just to make sure, you should post what that does.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org