随机产生50~100间的整数组成一个4行4列的矩阵,并求矩阵对角线元素的和.

问题描述:

随机产生50~100间的整数组成一个4行4列的矩阵,并求矩阵对角线元素的和.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a() As Integer, i, j, sum As Integer
ReDim ①
Label1.Text = "" : Label2.Text = ""
For i = 0 To 3
For j = 0 To 3
a(i, j) = ②
Label1.Text &= CStr(a(i, j)) & Space(5 - Len(CStr(a(i, j))))
If ③ Then sum +=④
Next
Label1.Text = ⑤
Next
Label2.Text = "对角线之和为:" & sum
End Sub
Private Sub Form1_Load(……) Handles Me.Load
Randomize( )
End Sub

ReDim a(3,3) '①
a(i,j) = Int(Rnd() * 51 + 50) '②
If i = j Then sum += a(i,j) '③④
Label1.Text = Label1.Text & vbCrLf '⑤