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

问题描述 投票:0回答:1

当我执行下面的代码时,它给出了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);
    }
}
java output logic
1个回答
1
投票

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;

© www.soinside.com 2019 - 2024. All rights reserved.