求解一道VB程序设计题:定义一个二维数组a,共4行4列。要求如下。

2024-12-03 07:50:25
推荐回答(2个)
回答1:

Private Sub Command1_Click()
Dim abc(3, 3) As Integer
Dim abcMax As Integer
Dim abcMin As Integer
Dim i As Integer
Dim j As Integer
Dim iMax As Integer
Dim jMax As Integer
Dim iMin As Integer
Dim jMin As Integer
Dim abcSum As Integer
Dim abcAve As Single
For i = 0 To 3
For j = 0 To 3
abc(i, j) = 50 + Int(Rnd(1) * 50)
Next j
Next i
abcMax = 0
abcMin = 100
For i = 0 To 3
For j = 0 To 3
If abc(i, j) > abcMax Then
abcMax = abc(i, j)
iMax = i
jMax = j
End If

If abc(i, j) < abcMin Then
abcMin = abc(i, j)
iMin = i
jMin = j
End If
Next j
Next i
abcSum = 0
For i = 0 To 3
For j = 0 To 3
abcSum = abcSum + abc(i, j)
Next j
Next i
abcAve = abcSum / 16
Me.FontSize = 20
For i = 0 To 3
For j = 0 To 3
Me.Print abc(i, j),
Next j
Me.Print
Next i
Me.Print "Max", iMax, jMax, abcMax
Me.Print "Min", iMin, jMin, abcMin
Me.Print "Sum", abcSum
Me.Print "Ave", abcAve
End Sub

回答2:

Private Sub Command1_Click()
Dim x(3, 3) As Integer
For i = 0 To 3
For j = 0 To 3
x(i, j) = 50 + Int(Rnd() * 51)
Next j
Next i
Cls
For i = 0 To 3
For j = 0 To 3
Print x(i, j),
Next j
Print
Next i
'------------------
Max = x(0, 0)
pmxi = 0
pmxj = 0
'------------------
Min = x(0, 0)
pnxi = 0
pnxj = 0
Sum = 0
For i = 0 To 3
For j = 0 To 3
Sum = Sum + x(i, j)
If x(i, j) > Max Then
Max = x(i, j)
pmxi = i
pmxj = j
End If
Next j
Print
Next i
For i = 0 To 3
For j = 0 To 3
If x(i, j) < Min Then
Min = x(i, j)
pnxi = i
pnxj = j
End If
Next j
Print
Next i
Print Max, pmxi, pmxj
Print Min, pnxi, pnxj
Print Sum, Sum / 16
End Sub