[英]Java Operator Precedence Expression Evaluation
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; 变量e是一个表达式语句,应返回表达式中被撤消变量的值;
What is the order of precdence of the opperators in the e variable? e变量中运算符的优先顺序是什么?
Computed it equals = 60; 计算得出等于60;
With a calculator I get 422; 使用计算器,我得到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;
Actually the answer is 60.1
but since variables
are int its showing 60
. 实际上答案是60.1
但是由于variables
int表示为60
。 It is happening as below 它发生如下
10 + (2 * (20 / 1)) + 10 + (int)(2 / 20) = 10 + (2 * 20) + 10 + (int)0.1
= 10 + 40 + 10 + 0 = 60
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
). 至于您的结果,这也可以归因于整数除法(它取结果的下限;例如2/20 = 0
)。
Just like in school, multiplication and division have priority over addition. 就像在学校一样,乘法和除法优先于加法。 So you have: 所以你有了:
10 + 2 * 20 / 1 + 10 + 2 / 20 = 10 + 40 + 10 + 0 = 60
* takes first precedence so first, 2*20 =40, 10 + 40 / 1 + 10 + 2 / 20;
/ takes precedence so , 10 + 40 + 10 + 0;
+ takes precedence so, 60
Here is link for operator precedence: Operator precedence 这是运算符优先级的链接: 运算符优先级
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.