某人可以帮我纠正我的vb代码吗?

时间:2013-11-17 00:28:29

标签: vba vb6

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.它有什么用吗?

1 个答案:

答案 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."