如何比较数组的元素

时间:2013-10-24 02:44:42

标签: arrays vb.net

我正在尝试创建一个for循环来遍历数组并使用临时变量执行基本开关以循环输出最大数字。我还需要打印元素在数组中的位置。这是我到目前为止所做的。

Dim highest As Decimal = gasArray(0)
Dim j As Integer = 1

For a As Integer = 0 To 11 Step 1
            If gasArray(a) < gasArray(a + 1) Then
                highest = gasArray(a + 1)
                a = j
            End If

        Next


        avgPriceLbl.Text = "$" & highest & " in month " & Array.FindIndex(j)

2 个答案:

答案 0 :(得分:1)

您需要的是以下内容:

 j = 0
 highest = gasArray(j)
 For a As Integer = 1 To 11 Step 1
      If highest < gasArray(a) Then
            highest = gasArray(a)
            j = a
        End If

    Next

答案 1 :(得分:1)

好的,你是新手,希望评论有意义......

' create some dummy data
Dim gasArray() As Decimal = New Decimal() {1, 7, 23, 11, 57, 0}

Dim highest As Decimal
Dim index As Integer

' it's better to ask an array what the bounds are 
For a As Integer = gasArray.GetLowerBound(0) To gasArray.GetUpperBound(0) Step 1
    If highest < gasArray(a) Then
        highest = gasArray(a)
        index = a
    End If
Next

' this is a prettier way to create strings
avgPriceLbl.Text As String = String.Format("${0} in month {1}", highest, index)