我正在编写一个Ceasaer函数,它接受一个字符串并通过Ceasear密码的变体运行它,并返回编码的文本。出于某种原因,我在声明没有特定边界的数组上得到索引超出范围错误。为什么我会遇到此异常,我该如何解决?
VB.NET代码:
Public Shared Function Ceaser(ByVal str As String) As String
Dim r As String = ""
Dim ints() As Integer = {}
Dim codeints As Integer() = {}
Dim codedints As Integer() = {}
Dim ciphertext As String = ""
For i As Integer = 0 To str.Length - 1
Dim currentch As Integer = Map(str(i))
ints(i) = currentch 'Where exception is happening
Next
Dim primes As Integer() = PrimeNums(ints.Length)
For i As Integer = 0 To primes.Length - 1
codeints(i) = ints(i) + primes(i) - 3
Next
For i As Integer = 0 To codeints.Length - 1
Dim currentnum As Integer = codeints(i) Mod 27
codedints(i) = currentnum
Next
For i As Integer = 0 To codedints.Length - 1
Dim letter As String = rMap(codeints(i))
ciphertext += letter
Next
Return ciphertext
End Function
答案 0 :(得分:1)
您必须先指定数组边界才能访问其元素:
Dim ints As Integer(str.length-1)
将使用n个元素实例化数组,其中n = string str的长度。
(注意:VB .NET数组长度从零开始,因此使用array(0)
实例化包含1个元素的数组。
您必须相应地采用其他数组。