前/后递增/递减和操作员订单混淆 [英] Pre/post increment/decrement and operator order confusion
本文介绍了前/后递增/递减和操作员订单混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在进行一些练习,但我对此感到困惑:
I was going through some exercises but I am confused in this one:
public static int f (int x, int y) {
int b=y--;
while (b>0) {
if (x%2!=0) {
--x;
y=y-2;
}
else {
x=x/2;
b=b-x-1;
}
}
return x+y;
}
b = y--的目的是什么?
?
所以,例如,当我们第一次去的时候, x = 5
和 y = 5
while循环内部(而(b> 0)
) b
= 4或5?当我在我的电脑中运行代码 b
是5.并且返回是3.我真的不清楚。很抱歉,如果我的问题不清楚。
What is the purpose of b=y--
?
So, for example, x=5
and y=5
when we first go inside of while loop (while (b>0)
) will b
= 4 or 5? When I am running the code in my computer b
is 5. And the return is 3. It is really unclear to me. Sorry if I am unclear in my question.
推荐答案
int b = y - ;
第一个分配 b = y
然后减少 y
( y -
)。
int b=y--;
first assignes b=y
and then decrements y
(y--
).
另请查看前缀/后缀一元增量运算符。
此示例(取自链接页面)演示它:
This example (taken from the linked page) demonstrates it:
class PrePostDemo {
public static void main(String[] args){
int i = 3;
i++;
// prints 4
System.out.println(i);
++i;
// prints 5
System.out.println(i);
// prints 6
System.out.println(++i);
// prints 6
System.out.println(i++);
// prints 7
System.out.println(i);
}
}
这篇关于前/后递增/递减和操作员订单混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文