代码测试通过,源码如下:
Sub test()
Dim arrNum(4, 4) As Integer '定义5*5数组
Dim max '定义最大值
Dim sum1, sum2 '定义正反斜角之和
'生成一个5*5二维数组
For i = 0 To 4
For j = 0 To 4
arrNum(i, j) = Rnd() * 100 '生成一个1~100的随机数
Next j
Next i
'求数组最大值
max = arrNum(0, 0)
For i = 0 To 4
For j = 0 To 4
If arrNum(i, j) > max Then max = arrNum(i, j) '只要当前元素大于max就将当前元素存入max
Next j
Next i
'求对角线之和
For i = 0 To 4
sum1 = sum1 + arrNum(i, i)
sum2 = sum2 + arrNum(i, 4 - i)
Next i
'输出结果
Dim arrStr, maxStr, sum1Str, sum2Str As String
arrStr = "数组元素如下:" & Chr(10) & Chr(13)
For i = 0 To 4
For j = 0 To 4
arrStr = arrStr & arrNum(i, j) & " "
Next j
arrStr = arrStr & Chr(10) & Chr(13)
Next i
maxStr = "数组最大值为: " & max & Chr(10) & Chr(13)
sum1Str = "正斜角元素之和为: " & sum1 & Chr(10) & Chr(13)
sum2Str = "反斜角元素之和为: " & sum2 & Chr(10) & Chr(13)
MsgBox arrStr & maxStr & sum1Str & sum2Str
End Sub