“值”“值”在java中

发布于 2024-12-01 22:18:28 字数 407 浏览 3 评论 0原文

可能的重复:
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 技术交流群。

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

发布评论

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

评论(8

孤千羽 2024-12-08 22:18:28

因为表达式 nir++ 的计算结果为 nir 的旧值。所以 exp(nir++) 相当于:

int oldValue = nir;
nir = nir + 1;
exp(oldValue);

在你的情况下,这意味着:

int oldValue = nir;
nir = nir + 1;
nir = oldValue;

这当然没有意义,因为它基本上什么也不做。

Because the expression nir++ evaluates to the old value of nir. So exp(nir++) is equivalent to:

int oldValue = nir;
nir = nir + 1;
exp(oldValue);

In your case that means:

int oldValue = nir;
nir = nir + 1;
nir = oldValue;

Which of course makes no sense because it basically does nothing.

雪落纷纷 2024-12-08 22:18:28

nir++ 返回 nir 值并随后增加它。因此,当 nir 等于 5 时,nir++ 返回 5 并将 nir 设置为 >6。之后,您将 nir 设置为 5,因此它返回 5。只需跳过 nir= 或使用 ++nir

nir++ returns nir value and increases it after that. So nir++ when nir is equal 5 returns 5 and sets nir to 6. After that you set nir to 5, so it returns 5. Just skip nir= or use ++nir.

夜夜流光相皎洁 2024-12-08 22:18:28

因为 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.

只是偏爱你 2024-12-08 22:18:28

尝试 nir = ++nir;情况会有所不同。
它首先会+1,然后将值设置为nir。

Try nir = ++nir; It will be different.
It will firstly +1 then set the value to nir.

潜移默化 2024-12-08 22:18:28

您不必使用 ++ 增量重新分配变量。

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 to nir.

If you want the value to be incremented and then used you can use pre incrementation ++nir.

始终不够爱げ你 2024-12-08 22:18:28

发生的情况如下:

int nir = 5;

nir 设置为 5

nir = nir++;

右侧被 nir 的当前值替换,然后计算 nir++。
换句话说:

nir = nir++;
//simplifies to (not executed!)
nir = nir;
//simplifies to (not executed!)
nir = 5;

执行Nextnir++。 nir 现在是 6

接下来执行 nir = 5;。尼尔现在是 5

This is what happens:

int nir = 5;

nir is set to 5

nir = nir++;

The right hand side is replaced by the current value of nir, and then nir++ is evaluated.
In other words:

nir = nir++;
//simplifies to (not executed!)
nir = nir;
//simplifies to (not executed!)
nir = 5;

Nextnir++ is executed. nir is now 6

Next nir = 5;is executed. nir is now 5

所有深爱都是秘密 2024-12-08 22:18:28

我有点惊讶地发现没有人(除了解释问题之外)提出了一个不那么晦涩难懂的不同运算符,至少对我来说是这样——尽管我已经从事 C 和/或 Java 30 多年了:

nir += 1;

这个的好处是它看起来更像是它(或 ++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:

nir += 1;

The nice thing about this is that it looks a lot more like the assignment that it (or ++nir) is.

安人多梦 2024-12-08 22:18:28

您的行:

nir = nir++;

意思是“将 nir (5) 的值分配给变量 nir,然后将该值增加 1。此增量操作在此过程中丢失。因为 nir 被覆盖。

您想要:

nir++;

或者

nir = ++nir;

如果您有

int a = 5;
int b = a++;

a 将设置为 6,b 将设置为 5。

Your line:

nir = nir++;

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:

nir++;

or

nir = ++nir;

If you instead had

int a = 5;
int b = a++;

a would get set to 6 and b would be 5.

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