You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Visual Basic实现无需用户手动重启即可让程序从头运行的方法

实现方案

核心逻辑

把所有验证流程包在无限循环内,验证失败时直接跳转回循环起点即可实现不重启程序重新运行的效果,同时修复原代码中存在的变量类型定义错误(原代码将密码变量定义为整数,用户输入非数字内容会直接报错)。

修改后完整代码

Module vbModule
    Sub Main()
        ' 外层无限循环作为程序入口点
        Do
            'Welcome Text
            Console.WriteLine("Welcome To Squidwards Password Checker")

            'Length Check
            'Creating The Variables
            ' 修正原错误:密码改为字符串类型,支持输入非数字内容
            Dim userPassword As String
            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 = 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.Length >= 8 Then
                Console.WriteLine("Your Password Was Long Enough")
            Else
                Console.WriteLine("Your Password Was Too Short")
                Console.WriteLine("请按回车键重启验证流程")
                Console.ReadLine()
                ' 直接跳转回循环开头
                Continue Do
            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")
            ' 增加输入合法性判断,避免用户输入非数字时程序崩溃
            If Not Integer.TryParse(Console.ReadLine(), num) OrElse num <7 Then
                Console.WriteLine("You Did Not Input A Number Greater Then 6, Please Try Again.")
                Console.WriteLine("请按回车键重启验证流程")
                Console.ReadLine()
                Continue Do
            Else
                Console.WriteLine("Human Verification Accepted.")
            End If

            'Presence Check
            Dim blankData 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 String.IsNullOrWhiteSpace(blankData) Then
                Console.WriteLine("You Did Not Input Anything, Please Repeat The Verification Proccess.")
                Console.WriteLine("请按回车键重启验证流程")
                Console.ReadLine()
                Continue Do
            Else
                Console.WriteLine("Inputted Characters Confirmed")
            End If

            ' 所有验证全部通过,跳出循环结束程序
            Console.WriteLine("所有验证已通过,程序运行结束")
            Exit Do
        Loop
    End Sub
End Module

关键改动说明

  • 外层增加Do...Loop无限循环包裹所有主逻辑,作为程序重启的入口
  • 每一项验证失败的分支中,增加按回车提示,调用Console.ReadLine()等待用户输入回车后执行Continue Do跳回循环开头
  • 修正原代码密码变量类型错误,避免非数字输入崩溃
  • 范围验证增加Integer.TryParse判断,避免用户输入非数字内容时程序抛出异常
  • 所有验证通过后执行Exit Do跳出循环,正常结束程序

内容的提问来源于stack exchange,提问作者SexySquidward458

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 13:18:03