该代码如何工作,输出是什么。.这看起来很简单

时间:2019-04-14 05:49:30

标签: java output logic

当我执行以下代码时,它给我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);
    }
}

1 个答案:

答案 0 :(得分: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;