如何根据用户输入的密码长度变量循环调用随机字符生成子程序?
Hey there! Let's sort out your password generator code so it properly gets the user's desired length and loops to build the password. Here's how to fix and enhance your existing code:
问题分析
First, let's look at the gaps in your current code:
- The
nvariable frominp()is a string (sinceConsole.ReadLine()returns a string), but we need an integer to control the loop. - Calling
Randomize()every time inrandi()can lead to less random results—it should only run once at the start of your program to properly seed the generator. - The
randi()sub doesn't actually return a random character, just sets an indexi; we need to map that index to your character library and output the character. - There's no validation for the user's input (e.g., ensuring it's a number between 1 and 20).
解决方案
Let's rewrite and integrate your code step by step:
1. Fix User Input Handling
Modify the input sub to convert the user's input to an integer, add validation, and ensure it's within the allowed range:
' Global variables (or pass them between subs if you prefer) Dim n As Integer ' Example character library—adjust this to include/exclude characters as needed Dim charLibrary As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()" Public Sub GetPasswordLength() Dim input As String Do Console.WriteLine("How many characters do you want in your password? (1-20 allowed)") input = Console.ReadLine() ' Try to convert input to integer and check if it's in valid range If Integer.TryParse(input, n) AndAlso n >= 1 AndAlso n <= 20 Then Exit Do ' Valid input, exit loop Else Console.WriteLine("Invalid input! Please enter a number between 1 and 20.") End If Loop End Sub
2. Optimize Random Character Generation
Move Randomize() to the start of your program, and change randi() to a function that returns a single random character from your library:
Public Function GetRandomCharacter() As Char ' Calculate a valid index within the character library bounds Dim randomIndex As Integer = CInt(Rnd() * (charLibrary.Length - 1)) Return charLibrary(randomIndex) End Function
3. Integrate the Loop to Build the Password
Create a main sub that ties everything together:
Sub Main() Randomize() ' Initialize random generator ONCE at program start GetPasswordLength() ' Get valid length from user Dim password As String = "" ' Loop n times to append random characters to the password For i As Integer = 1 To n password &= GetRandomCharacter() Next Console.WriteLine($"Your generated password is: {password}") Console.ReadLine() ' Keep console open to view the result End Sub
Key Notes
- Input Validation: The
Do...Loopensures the user can't proceed until they enter a valid number in the 1-20 range. - Randomness: Calling
Randomize()once at the start ensures the random generator is properly seeded for more reliable random results. - Customization: Adjust the
charLibrarystring to include any symbols, letters, or numbers you want in your passwords.
内容的提问来源于stack exchange,提问作者It was me DIO
相关产品推荐
相关产品推荐

