2017-12-14 11 views
-1

私は、線形探索を実装しようとしていますが、私はlinearSearch()サブルーチンに移動するたびに、私はエラーを取得する:線形探索アレイエラー

Index was outside the bounds of the array

このエラーを与えてラインがIf list(a) = numberToFind Thenを含むものです。これをどうすれば解決できますか?

Module Module1 

Sub Main() 

    Dim list(99) As Integer 
    Dim x As Integer = 0 
    Dim answer As Integer 

    Console.Write("Enter a value, type 9999 to stop.") 
    answer = Console.ReadLine() 

    For i = 0 To list.Length 

     If answer = 9999 Then 
      linearSearch(list) 
     Else 
      list(i) = answer 
      Console.Write("Enter another") 
      answer = Console.ReadLine 

     End If 
    Next 

End Sub 

Sub linearSearch(ByVal list) 

    Dim numberToFind, comparisonNo As Integer 
    comparisonNo = 0 

    Console.Write("What number do you want to find?") 
    numberToFind = Console.ReadLine() 

    For a = 1 To list.Length 
     If list(a) = numberToFind Then 
      Console.Write(comparisonNo) 
     Else 
      comparisonNo += 1 
     End If 
    Next 
    Console.ReadLine() 
End Sub 

End Module 

答えて

3

"a = 1 To list.Length"の行を "a = 0 To list.Length - 1"に変更します。配列はゼロベースです。