2021年1月22日星期五

Unbelievable result when use java loop and subtraction

I use windows 10,jdk1.8.

I use this code to test my program:

public class Unbelievable {      public void test() {          int a = 5;          while ((a -= 2) > 0){              //nothing to do          }          System.out.println("result= " + a);      }        public static void main(String[] args) {          Unbelievable un = new Unbelievable();          for (int i = 0; i < 2000_0; i++) {              un.test();          }      }  }  

When I run the code.The output likes this:

result= -1  result= -1  result= -1  result= -1  ……  result= /  result= /  result= /  result= /  result= /  result= /  result= /  result= /  result= /  

There are 12821 result= -1 and 7179 result= /,I don't understand why the result is like this.I think all the result shoule be result= -1.

https://stackoverflow.com/questions/65854898/unbelievable-result-when-use-java-loop-and-subtraction January 23, 2021 at 09:33AM

没有评论:

发表评论