Dim age As Integer = 1
Do While 1980 + age <> age * age
lblResult.Text = "The solution is " & age & " years old."
age = age + 1
Loop
当我运行这个程序时,它会给44而不是45.它有什么用吗?
答案 0 :(得分:1)
问题是代码在之前更新lblResult.Text
它会增加age
,所以当age
从44增加到45时,循环退出而不更新标签显示age
的最终值。
要修复代码,请在递增lblResult.Text
后更新age
。虽然你可以在循环中这样做......
Do While 1980 + age <> age * age
age = age + 1
lblResult.Text = "The solution is " & age & " years old."
Loop
...在循环结束后只需更新标签一次就足够了:
Do While 1980 + age <> age * age
age = age + 1
Loop
lblResult.Text = "The solution is " & age & " years old."