我正在运行以下代码
int x=4;
int y=3;
double z=1.5;
z=++x/y*(x-- +2);
int t=(++x/y);
System.out.println(z); //7
想知道它是如何在什么时候产生 7 的
- (x-- +2) =6
++x/y=1.6666
3=6*1.6666=10
最佳答案
z=++x/y*(x-- +2);
被评估为:
z = ++x / y * (x-- + 2); // Substitute value of ++x, y and x--
= 5 / 3 * (5 + 2); // After this point, x will be 4. Evaluate parenthesized expr
= 5 / 3 * 7 // Now, left-to-right evaluation follows
= 1 * 7 // 5 / 3 due to integer division will give you 1, and not 1.66
和:
t = ++x / y; // x is 4 here
= 5 / 3
= 1
关于java - java中的代码执行顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21202688/