早教吧 育儿知识 作业答案 考试题库 百科 知识分享

VB编写程序,用数组建立一个8*8的矩阵,数组元素值是10到99的范围内随机整数,求.1:所有元素之和2:求各行元素之和.3.求主对角线元素之和.4.所有靠边元素之和

题目详情
VB编写程序,用数组建立一个8*8的矩阵,数组元素值是10到99的范围内随机整数,
求.1:所有元素之和
2:求各行元素之和.3.求主对角线元素之和.4.所有靠边元素之和
▼优质解答
答案和解析
Private Sub Command1_Click()
Dim a(1 To 8, 1 To 8) As Integer, i As Integer, j As Integer, bN As Integer
Dim allN As Integer, allrow(1 To 8) As Integer, djN As Integer
For i = 1 To 8
For j = 1 To 8
a(i, j) = 10 + Int(Rnd * 90)
Print a(i, j);
allN = allN + a(i, j)
allrow(i) = allrow(i) + a(i, j)
If j = 8 Then Print "行的和-->"; allrow(i);
If i = j Then djN = djN + a(i, j)
If i = 1 Or i = 8 Then
bN = bN + a(i, j)
ElseIf j = 1 Or j = 8 Then
bN = bN + a(i, j)
End If
Next
Print

Next
Print "所有元素和:"; allN
Print "主对角线和:"; djN
Print "所有边的和:"; bN
End Sub