为什么结果的最终值为11而不是9?

问题描述 投票:0回答:1
int x = 1;
int result = 3;

while ( x < 5 ) {
   result = result + 2;
   x = x + 1;
}

使用 Java 进行处理

预测最终结果将是 9,而不是最终结果是 11。不知道如何,因为当结果等于 11 x 等于 5 时,这将打破循环并使用结果的先前值。

java processing
1个回答
0
投票
x = 1 < 5 => result = result + 2 = 5
x = 2 < 5 => result = result + 2 = 7
x = 3 < 5 => result = result + 2 = 9
x = 4 < 5 => result = result + 2 = 11
x = 5 = 5 => break because 5<5 is not true
© www.soinside.com 2019 - 2024. All rights reserved.