gpt4 book ai didi

Why does this output "7 9 35 5 27" and not "7 35 5 9 27"?(为什么输出的是“7 9 35 5 27”而不是“7 35 5 9 27”?)

转载 作者:bug小助手 更新时间:2023-10-24 20:12:49 26 4
gpt4 key购买 nike



Here is the code:

以下是代码:


public static void main(String[] args) {
int x = 5;
int y = 7;
System.out.print(m(x, y) + " " + x + " " + m(y, x));
}

public static int m(int x, int y) {
x += 2;
System.out.print(x + " ");
y -= 2;
return (x * y);
}

I tried writing this down on paper and going through the steps. In the first method call, x has 2 added to it and becomes 7, then is printed. Y is subtracted by 2, becoming 5. Then x*y gets returned, which is 7*5 = 35. So I expect 7 35 for now. Then x is printed, so print a 5. Then in the second method call, 2 is added to x again making it 9. 2 is subtracted from y, making 3. Then x*y is returned, outputting 27.

我试着把它写在纸上,一步一步地走。在第一个方法调用中,x加上2,变成7,然后打印出来。Y减去2,等于5。然后返回x*y,即7*5=35。所以我现在预计是7点35分。然后打印x,所以打印一个5。然后在第二个方法调用中,再次将2加到x,使其为9。从y中减去2,得到3。然后返回x*y,输出27。


So in the end, I expect 7 35 5 9 27.

所以最后,我预计是7 35 5 9 27.


更多回答

"So I expect 7 35 for now. " I don't understand. Why do you think the 35 should be printed at this point? "Then x is printed, so print a 5." I don't understand. Why do you think this print should happen before the second method call?

“所以我现在预计是7:35。”我不明白。你为什么认为35应该在这个时候印刷?然后打印x,因此打印一个5。我不明白。为什么您认为此打印应该在第二个方法调用之前进行?

No, x & y isn't returned. You return a value of their multiplication, which you never use anyway. Java also doesn't have any syntax to pass value by reference, so unless you explicitly return them (perhaps as part of a custom class) and assign them back to the caller, x and y shouldn't change

不,不返回x&y。您返回它们的乘法的值,但无论如何都不会使用它。Java也没有任何通过引用传递值的语法,所以除非您显式地返回它们(可能是作为定制类的一部分)并将它们分配回调用者,否则x和y不应该改变

This might help: stackoverflow.com/questions/40480/…

这可能会有帮助:Stackoverflow.com/Questions/40480/…

@OldDogProgrammer I think OP understands that x and y are not passed by reference. If they weren't, they would have expected the second m call to also return 35.

@OldDogProgrammer我认为OP理解x和y不是通过引用传递的。如果不是,他们会预计第二个m调用也会返回35。

@harold, Yes, I know that, but OP says "Then in the second method call, 2 is added to x again making it 9. 2 is subtracted from y, making 3", the misunderstanding is theirs

@harold,是的,我知道,但是OP说“然后在第二个方法调用中,2再次添加到x,使其成为9。y减去2,得到3”,误解是他们的

优秀答案推荐


So I expect 7 35 for now



That doesn't make sense. Unless you think that java will print each element in m(x, y) + " " + x + " " + m(y, x) 'on the fly'.

那也太没道理了。除非您认为Java会动态打印m(x,y)+“”+x+“”+m(y,x)‘中的每个元素。


That isn't how it works.

事情不是这样运作的。


The compiler first treats this statement as:

编译器首先将此语句视为:



  • Invoke m, passing 'x' and 'y'. This prints 7 , and resolves to 35.

  • resolve the binary operator of _string concat (A,B) where A is the result of the previous bullet (35) and B is " ". It doesn't get printed. Why would it be? We're just working on figuring out what m(x, y) + " " is, we haven't at all gotten to the idea that this is passed to System.out.print yet.

  • This resolves to 35 .

  • Next we resolve CONCAT(A, B) where A is that (35 ) and B is x, which resolves to 5. local variables and parameters are unique to each method, the fact that m changes x is just changing its own copy of x, not main's copy of it, so x is still 5. The string we are building up is now 35 5. This still isn't printed for the same reason.

  • Next we resolve CONCAT(A, B) where A is that (35 5) and B is , giving us 35 5 .

  • Next we resolve CONCAT(A, B) where A is that (35 5 ) and B is m(y, x). To do that we have to invoke m first to know what that really is, so we do that.

  • In passing this prints 9 (remember how every method gets its own unique copy? you called m with (y, x), but the first argument is known as x in m, so, it's 7 when called, gets incremented to 9, is printed, and is then multiplied by 3 and returned: 27. So far we've printed 7 9 as part of m calls.

  • Finally we can resolve CONCAT(35 5 , 27) which gets us 35 5 27. Which is passed to System.out.print. Which prints it.


Thus: 7 9 35 5 27.

因此:7 9 35 5 27。


更多回答

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com