“值”“值”在java中
可能的重复:
java运算符++问题
public class A {
public static void main(String[] args) {
int nir = 5;
nir = nir++;
System.out.print(nir);
}
}
为什么输出是5而不是6?
请不要告诉我该怎么做才能得到 6.. 显然我能够得到 6,只是语法对我来说看起来不错,并且解释一下其中的错误会更好,谢谢。
Possible Duplicate:
java operator ++ problem
public class A {
public static void main(String[] args) {
int nir = 5;
nir = nir++;
System.out.print(nir);
}
}
Why is the output 5 and not 6?
Please don't tell me what to do in order to get 6.. obviously I am able to get to 6, it's just that the syntax looks fine to me and an explanation about what wrong with that will do better, thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
因为表达式
nir++
的计算结果为nir
的旧值。所以exp(nir++)
相当于:在你的情况下,这意味着:
这当然没有意义,因为它基本上什么也不做。
Because the expression
nir++
evaluates to the old value ofnir
. Soexp(nir++)
is equivalent to:In your case that means:
Which of course makes no sense because it basically does nothing.
nir++
返回nir
值并随后增加它。因此,当nir
等于5
时,nir++
返回5
并将nir
设置为>6
。之后,您将nir
设置为5
,因此它返回5
。只需跳过nir=
或使用++nir
。nir++
returnsnir
value and increases it after that. Sonir++
whennir
is equal5
returns5
and setsnir
to6
. After that you setnir
to5
, so it returns5
. Just skipnir=
or use++nir
.因为 nir++ 在递增 nir 之前先获取它的值,所以如果您希望该值为 6,请使用 ++nir,它表示在给出值之前递增 nir。
Because nir++ gets the value of nir before incrementing it, if you want the value to be 6 use ++nir, which states increment nir before giving the value.
尝试 nir = ++nir;情况会有所不同。
它首先会+1,然后将值设置为nir。
Try nir = ++nir; It will be different.
It will firstly +1 then set the value to nir.
您不必使用
++
增量重新分配变量。nir++
是后递增:首先将变量用于表达式,然后递增。因此,在这里您将值 5 分配给nir
。如果您希望该值先递增然后使用,可以使用预递增
++nir
。You don't have to reassign the variable using
++
incrementation.nir++
is post incrementation : first the variable is used into the expression and then it is incremented. So here you assign the value 5 tonir
.If you want the value to be incremented and then used you can use pre incrementation
++nir
.发生的情况如下:
nir 设置为 5
右侧被 nir 的当前值替换,然后计算 nir++。
换句话说:
执行Next
nir++
。 nir 现在是 6接下来执行
nir = 5;
。尼尔现在是 5This is what happens:
nir is set to 5
The right hand side is replaced by the current value of nir, and then nir++ is evaluated.
In other words:
Next
nir++
is executed. nir is now 6Next
nir = 5;
is executed. nir is now 5我有点惊讶地发现没有人(除了解释问题之外)提出了一个不那么晦涩难懂的不同运算符,至少对我来说是这样——尽管我已经从事 C 和/或 Java 30 多年了:
这个的好处是它看起来更像是它(或 ++nir)的赋值。
I was a little surprised to see that no one had (in addition to explaining the problem) suggested a different operator that's a lot less obscure, at least for me - even though I've been doing C and/or Java for 30+ years:
The nice thing about this is that it looks a lot more like the assignment that it (or ++nir) is.
您的行:
意思是“将 nir (5) 的值分配给变量 nir,然后将该值增加 1。此增量操作在此过程中丢失。因为 nir 被覆盖。
您想要:
或者
如果您有
a
将设置为 6,b
将设置为 5。Your line:
Means "assign the value of nir (5) to variable nir and then increment the value by 1. This increment operation is lost in the process. Since nir gets overwritten.
You want:
or
If you instead had
a
would get set to 6 andb
would be 5.