Visual Basic 2010 编程中的循环应用与技巧
1. 登录程序示例
用户登录计算机是 Do Loop Until
循环的一个很好示例。用户输入登录信息以访问计算机、网络或网站,至少需要一次尝试,如果出错可能需要两到三次尝试。为防止重复失败尝试,代码如下:
Do
shoAttempts = shoAttempts + 1
strLogin = InputBox("Enter your login", "System Access")
strLogin = strLogin.ToUpper
If strLogin <> strAccess Then
MsgBox("Login failed", MsgBoxStyle.OkOnly)
End If
Loop Until strLogin = strAccess Or shoAttempts >= 3
'If login isn't correct, then end the program
If strLogin <> strAccess Then
End
Else
MsgBox("Welcome!", MsgBoxStyle.OkOnly)
End If
该循环至少运行一次,执行循环内代码后检查是否重复,直到输入正确登录信息或尝试次数超过限制。循环下方的 If
语句决定是否允许访问或结束程序。
2. 分析循环的关键问题
当看到代码中的循环时,需问自