当我执行下面的代码时,它给出了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;