VB 二维数组找最大元素位置,要求先设第一行第一列为最大元素,option base 1 求代码

1个回答

  • Option Base 1

    Private Sub Command1_Click()

    Dim a(4, 5) As Integer

    Dim i As Integer

    Dim j As Integer

    Dim v As Integer

    Dim h As Integer

    Picture1.Cls

    For i = 1 To 4

    For j = 1 To 5

    a(i, j) = Int(Rnd * 90) + 10

    Picture1.Print a(i, j);

    Next j

    Picture1.Print

    Next i

    Picture1.Print

    h = 1

    v = 1

    For i = 1 To 4

    For j = 1 To 5

    If a(h, v) < a(i, j) Then h = i: v = j

    Next j

    Next i

    Picture1.Print "数组中最大元素为第" h "行" "第" v "列的" a(h, v)

    End Sub

    Private Sub Command2_Click()

    End

    End Sub

    你的代码错在a(h, v) = a(1, 1)和a(h, v) = a(i, j),不但是多余的,而且会改变的数组a的值,造成错误.你说说,你干嘛要让a(h, v) = a(i, j)呢?你只需要记录最大元素的下标值即可,最后通过下标值就可以知道最大元素的值了啊!