0

Java 代码如下:

    int x = 10;
    while (x != 15){
       x = x++;
       System.out.println("Value X " + x);
    }

执行:无限循环?为什么 ?

4

6 回答 6

2

Because x will never change and always will be 10. Read JLS.

于 2013-04-08T09:55:01.027 回答
2

I may be wrong but x++ incrementes x after reading it so x = x++ means x = x. You should use x = ++x instead.

See this question

于 2013-04-08T09:55:12.307 回答
2

Becouse you assign the old value of x to x befor you increase the value! Try this:

int x = 10;
while (x != 15){
   x++;
}

or

int x = 10;
while (x != 15){
   x = ++x;
}
于 2013-04-08T09:55:27.407 回答
1

x++; increments the value of x then returns the old value of x.

THEN x = (that number) executes, meaning the mutation x++ did of x is undone.

于 2013-04-08T09:54:37.443 回答
1

该行x = x++;应该是x++;

于 2013-04-08T09:57:14.470 回答
0

Because x = x++ will assign the result of the operation x++ to x. What you want is only x++;, which increments the value of x by one.

于 2013-04-08T09:54:08.863 回答