gpt4 book ai didi

java - 这段代码是如何工作的以及输出是什么..这看起来很简单

转载 作者:行者123 更新时间:2023-12-02 10:00:25 25 4
gpt4 key购买 nike

当我执行下面的代码时,它给出了 13 作为答案。我不明白这是如何工作的?

package Assignment_One;

class Main {
public static void main(String[] args) {
int this_number = 3;
int that_number;

while(this_number<10) {
that_number=this_number;
this_number = that_number+this_number/2;
}
System.out.println("Answer is:" + this_number);
}
}

最佳答案

this_number 经历了以下更改:

-> 3
-> 3 + [int(3/2) = 1] = 4
-> 4 + [int(4/2) = 2] = 6
-> 6 + [int(6/2) = 3] = 9
-> 9 + [int(9/2) = 4] = 13

此后,违反了 while 循环下的条件,因此跳出了循环。

你能具体说一下哪部分是你不明白的吗?您是否有循环或以下行的问题:
this_number = that_number+this_number/2;

关于java - 这段代码是如何工作的以及输出是什么..这看起来很简单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55672327/

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