【发布时间】:2018-06-22 16:43:42
【问题描述】:
在我调用函数的行的 sub main 中,我遇到了一个我不知道如何解决的错误:
索引数超过索引数组的维数
Module Module1
Sub main()
Dim names() As String = {"heimdall", "hella", "loki", "thor", "tyr", "odin"} 'list in order
Dim last As Integer = names.Length - 1
Dim first As Integer = 0
Dim player As String = "thor"
search(names(), last, first, player)
End Sub
Function search(ByVal names() As String, ByVal last As Integer, ByVal first As Integer, ByVal player As String)
Dim midpoint As Integer = (first + last) \ 2
Dim found As Boolean = False
While found = False
If last < first Then
Return -1
End If
If names(midpoint) > player Then
Return search(names, last, first, midpoint)
found = True
Else
Return midpoint
found = True
End If
End While
Return midpoint 'automatically does this when found = true
End Function
End Module
【问题讨论】:
-
如果你打开 Option Strict 你会发现你的函数需要一个数据类型。此外,当您在第二个 Return 语句中递归调用函数时,您传递的中点数据类型错误。
标签: vb.net recursion binary-search