简体   繁体   中英

Why is this giving me this output?

public static void main(String[] args) {
         
           int n=0, m=0, i=0,maxI=300;

            while(i++<maxI) n++;

                   i=0;

                 while(++i<maxI) m++;

                System.out.print(n+","+m+","+i)
}

The result of this code, when i printout nm,i is 300, 299, 300. What happened? I cant seem to figure out how it came to the result. I was expecting only i to have increased in line 2. Its giving me headaches

Why shouldn't 'i' increase also in line 4? It's post-incrementation, but it is, still, an incrementation.

Check your indentations, Be Careful about your pre and post increment for i(++i. i++). Because of which you get 299 for i(++i)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM