vb.net - While 循环变量中的不一致
问题描述
我正在编写一个简单的加密器/解密器程序并且遇到了一个小问题。所有字符都被生成一个不同的字符串来唯一标识它们。这些字符串的长度应该相同,但似乎并非如此。有问题的代码:(完整代码可以在这里查看)
Dim genLength As Integer = 0
Dim charNow As Integer = 0
Dim charGenned As String
Dim rndGend As Integer = 0
While genLength < enhancedMode
fs.Write(arrayAll(charNow))
Dim rndString As String = ""
While rndGend < encLength
Randomize()
Dim genned As Integer = CInt(Math.Ceiling(Rnd() * 46)) + 1
charGenned = arrayAll(genned)
rndString = rndString + charGenned
rndGend += 1
End While
fs.Write(rndString & vbNewLine)
rndGend = 0
charNow = charNow + 1
genLength = genLength + 1
End While
据我所知,这应该给我我正在寻找的结果,但生成的字符串的长度根本不一致。输出示例:
alh*ph)lufe$2fz!d7c0$qfd(ol6f173#
b^i24@^v0gx%01iqrpugg8)(mqsl8%
c1km5jnz0hti&u$#rqeh5ism31t^96^
dkx&6$ok!@u#*e^x6659jpvcnn258zpi
e%y1(y3%@w9kk9&h7d6gw)w72*3c9*d)j
fy#(i4yeg0%ltj@887!x4!e32^703e4l
gj$4#5&f!!zzdkvs)v@@94)*rcmroy
字母 A 后面的字符串长度为 32 位,对于 B 和 C 也是如此,但当您到达 G 时,字符串长度仅为 29 个字符。最值得注意的是,程序只为数字 5 以内的字符生成字符串,然后停止:
3%y1(y3%@w9kk9&h7d6gw)w72*3c9*d)j
4y#(i4yeg0%ltj@887!x4!e32^703e4l
5j$4#5&f!!zzdkvs)
这里出了什么问题?
解决方案
我只是整理了一下,改成了我认为更容易使用的 Random 类。如果您在 vb.net 中声明一个数组,请记住它是 Dim variable(ubound) As Type。ubound 代表数组的上界,即最高索引,因此具有 47 个元素的数组的索引为 0-46。ubound 将是 46 Dim variable(46) As String
Private r As New Random
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Button1.Enabled = False
Dim arrayLetters() As String = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
Dim arrayAll() As String = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")"}
Dim encLength As Integer = 16
If CheckBox2.Checked = True Then
encLength = 32
End If
Dim enhancedMode As Integer = 26
If CheckBox1.Checked = True Then
enhancedMode = 46
End If
Dim genLength As Integer = 0
Dim charNow As Integer = 0
Dim charGenned As String
Dim rndGend As Integer = 0
Dim sb As New StringBuilder
While genLength < enhancedMode
sb.Append(arrayAll(charNow))
Dim rndString As String = ""
While rndGend < encLength
Dim genned As Integer = r.Next(0, 46)
charGenned = arrayAll(genned)
rndString &= charGenned
rndGend += 1
End While
sb.AppendLine(rndString)
rndGend = 0
charNow += 1
genLength += 1
End While
Dim name As String
If TextBox1.Text = "" Then
name = "KeyGenned"
Else
name = TextBox1.Text
End If
Dim path As String = Application.StartupPath & "\" & name & ".txt"
File.WriteAllText(path, sb.ToString)
Close()
End Sub
推荐阅读
- testing - gradle test --test 选项无法生成 xml 报告
- react-native - 如何将像素值添加到 100% 填充值
- javascript - 更改父窗口 iframe 的 src
- java - 无法在目标文件中存储和加载数组列表
- gcc - Gcc优化问题:unsigned mod vs if branch
- c# - Json.NET 中仅一个属性的自定义序列化程序,无需更改模型类
- java - 导航栏无法使用 onbackPressed 片段正确更新
- java - Java 多线程程序中的对象可见性
- reactjs - .Net Core SPA 和 React - Active Directory 登录后的 HTTP 400
- loops - 使用 Selenium 和 Robot Framework 遍历 Web 元素