這是我的代碼
Module vbModule
Sub Main()
'Welcome Text
Console.WriteLine("Welcome To Squidwards Password Checker")
'Length Check
'Creating The Variables
Dim userPassword As Integer
Dim passwordLength As String
'Asking The User To Enter A Password
Console.WriteLine("Enter A Password That Consists Of 8 Or More Characters")
userPassword = Console.ReadLine()
passwordLength = Convert.ToString(userPassword)
'Repeats Your Password To You, And Tells You It's Length
Console.WriteLine("You Entered: " passwordLength)
Console.WriteLine(passwordLength.Length())
'Checks If Your Password Is Long Enough
If passwordLength >= 8 Then
Console.WriteLine("Your Password Was Long Enough")
Else
Console.WriteLine("Your Password Was Too Short")
End If
Console.WriteLine("Please Complete The Next Verification Step")
'Next Verification Test
'Range Check
Dim num As Integer
'Asks The User To Input A Number Greater Than 6
Console.WriteLine("Please Write A Number Greater Than 6")
'Checks If The User Inputted A Number Greater Than 6
num = Console.ReadLine()
If num >= 7 Then
Console.WriteLine("Human Verification Accepted.")
'If They Dont Input A Number Greater Then 6 They Are Promted With This Statement
Else
Console.WriteLine("You Did Not Input A Number Greater Then 6, Please Try Again.")
End If
'Peresence Check
Dim blankData As String
Dim blankData2 As String
Console.WriteLine("Please Input Some Characters")
blankData = Console.ReadLine()
'Checks if user inputed something or nothing, if they dont they have to restart the verification proccess
If blankData = "" Then
Console.WriteLine("You Did Not Input Anything, Please Repeat The Verification Proccess.")
Else
Console.WriteLine("Inputted Characters Confirmed")
End If
End Sub
End Module
當他們沒有通過我想讓程式說的任何驗證時,按 Enter 重新啟動,它會將他們帶回開始
uj5u.com熱心網友回復:
使用該Continue陳述句,您可以跳過下面代碼的執行Continue并重新啟動回圈。此外,使用該Exit陳述句,您可以在滿意時離開回圈。
Sub Main()
While True
'Welcome Text
Console.WriteLine("Welcome To Squidwards Password Checker")
'Asking The User To Enter A Password
If Not ValidatePassword() Then Continue 'If check failed, restart the loop
Console.WriteLine("Please Complete The Next Verification Step")
'Range Check
If Not ValidateRange() Then Continue 'If check failed, restart the loop
'Peresence Check
If PresenceCheck() Then Exit While 'If check passed, exit the loop
End While
End Sub
我Boolean根據驗證是通過還是失敗將所有驗證檢查拆分為函式(回傳 true/false)。
一個示例函式看起來像
Private Function ValidatePassword() As Boolean
Console.WriteLine("Enter A Password That Consists Of 8 Or More Characters")
Dim userPassword As String = Console.ReadLine()
Dim passwordLength As Integer = userPassword.Length()
'Repeats Your Password To You, And Tells You It's Length
Console.WriteLine("You Entered: " userPassword)
Console.WriteLine(passwordLength)
'Checks If Your Password Is Long Enough
If passwordLength >= 8 Then
Console.WriteLine("Your Password Was Long Enough")
Return True
Else
Console.WriteLine("Your Password Was Too Short")
Return False
End If
End Function
檢查與您最初進行的檢查相同,其中包含一條Return指示成功:true或失敗的陳述句:false
uj5u.com熱心網友回復:
您可以將代碼包裝在 this Do ... Loop Whileand 中Try ... Catch,檢查驗證失敗時拋出的特定例外型別。
Dim restart = False
Do
Try
restart = False
' your code in here with failed validation throwing exception like so
'Welcome Text
Console.WriteLine("Welcome To Squidwards Password Checker")
'Length Check
'Creating The Variables
Dim userPassword As Integer
Dim passwordLength As String
'Asking The User To Enter A Password
Console.WriteLine("Enter A Password That Consists Of 8 Or More Characters")
userPassword = Console.ReadLine()
passwordLength = Convert.ToString(userPassword)
'Repeats Your Password To You, And Tells You It's Length
Console.WriteLine("You Entered: " passwordLength)
Console.WriteLine(passwordLength.Length())
'Checks If Your Password Is Long Enough
If passwordLength >= 8 Then
Console.WriteLine("Your Password Was Long Enough")
Else
Console.WriteLine("Your Password Was Too Short")
Throw New ArgumentException()
End If
' rest of your code with ArgumentExceptions thrown when validation fails...
Catch ex As ArgumentException
restart = True
End Try
Loop While restart
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/318591.html
標籤:网络
