gpt4 book ai didi

java - 我的函数中出现意外输出

转载 作者:行者123 更新时间:2023-12-02 07:06:52 26 4
gpt4 key购买 nike

public class Backhand {
int state = 0;

Backhand(int s) {
state = s;
}

public static void main(String... hi) {
Backhand b1 = new Backhand(1);
Backhand b2 = new Backhand(2);
System.out.println( b2.go(b2));
}

int go(Backhand b) {
if(this.state ==2) {
b.state = 5;
go(this);
}
return ++this.state;
}
}

当它运行时,它输出 7。我认为 ++this.state; 应该在方法 go 中只执行一次,输出应该是 6。有人能解释一下这里发生了什么吗?

最佳答案

之所以得到 7 是因为,您在 go 方法内部调用了 go(this),所以最后,state 增加两次。

关于java - 我的函数中出现意外输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15993405/

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