Java 运算符优先级表达式求值
我有以下变量减速度、赋值和变量减速度
变量 e 是一个表达式语句,它应该返回表达式中求值变量的值;
e 变量中运算符的优先顺序是什么?
计算出来等于= 60;
用计算器我得到422;
int a, b, c, d;
a = 10;
b = 2;
c = 1;
d = 20;
e = a + b * d / c + a + b / d;
e = 10 + 2 * 20 / 1 + 10 + 2 / 20;
e = 60;
I have the following variable decelrations, assignments and variable declerations
variable e is an expression statement which should return the value of the evaulated variables in the expression;
What is the order of precdence of the opperators in the e variable?
Computed it equals = 60;
With a calculator I get 422;
int a, b, c, d;
a = 10;
b = 2;
c = 1;
d = 20;
e = a + b * d / c + a + b / d;
e = 10 + 2 * 20 / 1 + 10 + 2 / 20;
e = 60;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
实际上答案是
60.1
,但由于变量
是int,因此显示60
。事情发生如下Actually the answer is
60.1
but sincevariables
are int its showing60
. It is happening as below这里是概述运算符优先级的链接。至于您的结果,这也可以归因于整数除法(它取结果的下限;例如,
2/20 = 0
)。Here is a link outlining operator precedence. As for your result, this can also be attributed to integer division (which takes the floor of the result; for instance,
2/20 = 0
).就像在学校一样,乘法和除法优先于加法。所以你有:
Just like in school, multiplication and division have priority over addition. So you have:
以下是运算符优先级的链接:运算符优先级
Here is link for operator precedence: Operator precedence