gpt4 book ai didi

java - java中的代码执行顺序

转载 作者:行者123 更新时间:2023-12-03 23:44:49 24 4
gpt4 key购买 nike

我正在运行以下代码

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 的

  1. (x-- +2) =6
  2. ++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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com