Java 运算符优先级表达式求值

发布于 2024-12-25 02:22:19 字数 282 浏览 1 评论 0原文

我有以下变量减速度、赋值和变量减速度

变量 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

怪异←思 2025-01-01 02:22:19

实际上答案是60.1,但由于变量是int,因此显示60。事情发生如下

10 + (2 * (20 / 1)) + 10 + (int)(2 / 20) = 10 + (2 * 20) + 10 + (int)0.1
= 10 + 40 + 10 + 0 = 60

Actually the answer is 60.1 but since variables are int its showing 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
谢绝鈎搭 2025-01-01 02:22:19

这里是概述运算符优先级的链接。至于您的结果,这也可以归因于整数除法(它取结果的下限;例如,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).

喜爱纠缠 2025-01-01 02:22:19

就像在学校一样,乘法和除法优先于加法。所以你有:

10 + 2 * 20 / 1 + 10 + 2 / 20 = 10 + 40 + 10 + 0 = 60

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
铃予 2025-01-01 02:22:19
* 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

以下是运算符优先级的链接:运算符优先级

* 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文