子程序中的数组声明

时间:2015-09-26 14:04:44

标签: vb.net multidimensional-array

我无法弄清楚这些代码存在哪些问题?

它不断产生:

  

IndexOutOfRangeException

Module Module1
Sub Main()
    Console.WriteLine("Please input the length of the array")
    Dim n As Integer = Console.ReadLine()
    Dim numbers(n - 1) As Integer
    Console.WriteLine("Please inform us the number you want to find")
    Dim x As Integer = Console.ReadLine()
    For i = 0 To n - 1
        Console.WriteLine("Please input the {0} number", i)
        numbers(i) = Console.ReadLine()
    Next
    examining(x, n, numbers(n - 1))
    Console.ReadLine()

End Sub

    Sub examining(ByVal x As Integer, ByVal n As Integer, ByVal ParamArray      numbers() As Integer)
    Dim i As Integer
    For i = 0 To n - 1
        If numbers(i) = x Then
            Console.WriteLine("There exists {0} in the array", x)
            Exit For
        End If
    Next
    If i = n - 1 Then Console.WriteLine("{0} does not exist in the array", x)

    End Sub
End Module

1 个答案:

答案 0 :(得分:0)

检查数组的简单方法包含:

If numbers.Contains(x) Then
 'it does
Else
 'it does not
End If