Java 代码如下:
int x = 10;
while (x != 15){
x = x++;
System.out.println("Value X " + x);
}
执行:无限循环?为什么 ?
Java 代码如下:
int x = 10;
while (x != 15){
x = x++;
System.out.println("Value X " + x);
}
执行:无限循环?为什么 ?
Because x will never change and always will be 10. Read JLS.
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
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;
}
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.
该行x = x++;应该是x++;
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.