我如何让我的代码从乞讨开始,而无需用户重新启动程序,Visual Basic

How do i make my code start from the begging without the user having to restart the program, Visual Basic

这是我的代码

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 键重新启动,它会让他们回到开头

您可以将您的代码包装在此 Do ... Loop WhileTry ... 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

使用 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 函数 (return 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