VB 二维数组找最大元素位置,要求先设第一行第一列为最大元素,option base 1 求代码Option Base 1Private Sub Command1_Click()Dim a(4, 5) As IntegerDim i As IntegerDim j As IntegerDim v As IntegerDim h As IntegerPicture1.ClsFor i
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/17 13:54:16
VB 二维数组找最大元素位置,要求先设第一行第一列为最大元素,option base 1 求代码Option Base 1Private Sub Command1_Click()Dim a(4, 5) As IntegerDim i As IntegerDim j As IntegerDim v As IntegerDim h As IntegerPicture1.ClsFor i
VB 二维数组找最大元素位置,要求先设第一行第一列为最大元素,option base 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
a(h, v) = a(1, 1)
For i = 1 To 4
For j = 1 To 5
If a(h, v) < a(i, j) Then a(h, v) = a(i, j): h = i: v = j
Next j
Next i
Picture1.Print "数组中最大元素为第" & h & "行" & "第" & v & "列的" & a(h, v)
End Sub
Private Sub Command2_Click()
End
End Sub
我写的会溢出
VB 二维数组找最大元素位置,要求先设第一行第一列为最大元素,option base 1 求代码Option Base 1Private Sub Command1_Click()Dim a(4, 5) As IntegerDim i As IntegerDim j As IntegerDim v As IntegerDim h As IntegerPicture1.ClsFor i
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)呢?你只需要记录最大元素的下标值即可,最后通过下标值就可以知道最大元素的值了啊!