It's not clear why this run result is 0

dtongwei 注册会员
2023-02-28 06:24

Yes, I posted the photo of the title in the question, you can see

devil_2103 注册会员
2023-02-28 06:24

The answer part references GPT, GPT_Pro better solve the problem
The result of the above code is 0 because the variable i starts with 11, and in the do-while loop, The value of i will be increased by 2, but when 88 is greater than i, the while condition is not satisfied, the loop terminates, and the final result is the value of i, that is, 11+2+31=44, while 44 is less than 88, so the final result is 0.

The specific code is as follows:

int i = 11;
int sum = 0;
do {
    sum = sum + i;
    i += 2;
} while (i <= 88);

In the above code, we first define a variable i to store the initial value of the loop 11; Define a variable called "sum" to store the sum of the loop. Then we use a do-while loop to add the value of "sum" to the value of "i", and increment the value of "i" by 2 each time the loop is complete. When i is greater than 88, the while condition is not satisfied and the loop terminates. Finally, the value of sum is printed out.
If the answer is helpful, please accept it.

czytjkp 注册会员
2023-02-28 06:23

Is this code OK for this problem? Because the code itself is good, but the int can't store that much

chengcyx 注册会员
2023-02-28 06:23
< div class = "md_content_show e397 data - v - 3967" = "" >

your topic is for 88! ??

daizhenbin168 注册会员
2023-02-28 06:23
  • The maximum value of Int type in Java is 2147483647, above i < 10, a = 1073741824; i < At 11:00, a has already overflowed, and this overflow is a lot, you can see the screenshot below


  • i < 1073741824 * 3628800 = 3896394330931200 when the value of a is 11, the conversion of the base is as follows:


  • The 32-bit binary data where the int data resides is all 0, so the final output of a is 0

About the Author

Question Info

Publish Time
2023-02-28 06:23
Update Time
2023-02-28 06:23

Related Question