简体   繁体   English

有人可以解释为什么答案是B? 我正在使用Java,我不明白

[英]Can someone explain why the answer is B? I'm using Java and I don't understand

Which of the following statements are the same? 以下哪项陈述相同?

(I) x -= x + 4 (I)x - = x + 4

(II) x = x + 4 - x (II)x = x + 4 - x

(III) x = x - (x + 4) (III)x = x - (x + 4)

A. (I) and (II) are the same A.(I)和(II)是相同的

B. (I) and (III) are the same B.(I)和(III)是相同的

C. (II) and (III) are the same C.(II)和(III)是相同的

D. (I), (II), and (III) are the same D.(I),(II)和(III)是相同的

x -= y is equivalent to x = x - y

Therefore 因此

x -= x + 4

is equivalent to 相当于

x = x - (x+4)

So assuming (II) x = x - (x + 4) was supposed to be (III) x = x - (x + 4) (since you have two options marked as (II) ), (I) and (III) are the same. 所以假设(II) x = x - (x + 4)应该是(III) x = x - (x + 4) (因为你有两个选项标记为(II) ), (I)(III)是相同的。

It's because of operator precedence. 这是因为运营商的优先权。 Java evaluates that as if it were Java评估它就好像它一样

x -= (x+4) x - =(x + 4)

so it first computes (x+4) and then subtracts that from x -- which is what the - part of -= means -- and then updates x , which is what the = part means. 所以它首先计算(x+4)和然后减去从x -这是什么-的一部分-=指-然后更新x ,这是什么=部装置。

  • Case (I) expands to x = x − (x + 4) according to the Java -= operator, 情况(I)根据Java -=运算符扩展到x = x - (x + 4),
    and mathematically simplifies to x = −4. 并在数学上简化为x = -4。
  • Case (II) mathematically simplifies to x = 4. 情况(II)在数学上简化为x = 4。
  • Case (III) mathematically simplifies to x = −4. 情况(III)在数学上简化为x = -4。

Therefore (I) and (III) are the same, which means the answer is (B). 因此(I)和(III)是相同的,这意味着答案是(B)。

-= is a so called compound assignment. -=是所谓的复合赋值。

Those are just short-cuts and combine atomic operations. 这些只是捷径,并结合原子操作。

x -= y stands for x = xy x -= y代表x = xy

x += y stands for x = x+y x += y代表x = x+y

x++ stands for x = x+1 x++代表x = x+1

x-- stands for x = x-1 x--代表x = x-1

There also are ++x and --x which do the same as x++ / x-- except that they return the value of x before it is incremented / decremented. 还有++ x和--x与x ++ / x--相同,只是它们递增/递减之前返回x的值。

Official Java tutorial: 官方Java教程:

"You can also combine the arithmetic operators with the simple assignment operator to create compound assignments. For example, x+=1; and x=x+1; both increment the value of x by 1." “您还可以将算术运算符与简单赋值运算符组合以创建复合赋值。例如,x + = 1;并且x = x + 1;两者都将x的值递增1。”

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html

I think the same works for *= , /= and %= 我认为同样适用于*=/=%=

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有人可以解释为什么我在Java中遇到此错误吗? - Can someone explain why I'm getting this error in Java? 有人可以帮助我理解为什么我需要IF和WHILE来获得这个答案吗? - Can someone help me understand why I need an IF and WHILE for this answer? 有人可以解释为什么我在这段代码中收到错误 - Can someone please explain why I'm getting an error in this code 有人可以向我解释为什么我有此错误吗? - Can someone explain to me why I'm having this error? 我不明白为什么我的手工回答不同 - I don't understand why my answer by hand is different 在 Java 中使用 generics,有人可以解释我做错了什么 - Using generics in Java, can someone please explain what I'm doing wrong 我得到一个ArrayIndexOutOfBounds异常,我不知道为什么。 有人可以解释一下吗? - I'm getting an ArrayIndexOutOfBounds Exception and I don't know why. Can someone shed some light? 有人可以帮我解释为什么我不能显示适当的中位数吗? 【JAVA] - Can someone help explain why I can't display a proper median? [Java] 我正在使用一个程序来分析我的Java代码,但我不理解该错误或如何解决它 - I'm using a program to analyse my Java code and I don't understand the error or how to fix it 我使用的是我不完全了解的内容,是吗? - I'm using something i don't entirely understand, clarification?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM