0

我正在尝试创建一个 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)
4

2 回答 2

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
于 2013-10-24T02:54:00.373 回答
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)
于 2013-10-24T03:03:22.720 回答