vb程序设计的题目:
答案:3 悬赏:60 手机版
解决时间 2021-07-21 09:58
- 提问者网友:记得曾经
- 2021-07-20 10:34
设有一个二维数组A(5,5),试编写程序计算: 所有元素的和, 所有靠边元素的和, 两条对角线元素之和
最佳答案
- 五星知识达人网友:孤独入客枕
- 2021-07-20 11:57
'只要数组为n*n的都适用,只要修改n就行
Option Explicit
Private Sub Command1_Click()
Const n = 5
Dim i As Integer, j As Integer, arr(1 To n, 1 To n) As Long, a, b, c, d
For i = LBound(arr, 1) To UBound(arr, 1)
For j = LBound(arr, 2) To UBound(arr, 2)
arr(i, j) = 1
a = a + arr(i, j)
If i = j Then b = b + arr(i, j)
If j = UBound(arr, 1) - i + 1 Then b = b + 1
If i = LBound(arr, 1) Or i = UBound(arr, 1) Or j = LBound(arr, 2) Or j = UBound(arr, 2) Then c = c + 1
Next
Next
MsgBox "总计:" & a & vbNewLine & "2条对角线之和:" & b & vbNewLine & "4周数字之和" & c
End Sub
Option Explicit
Private Sub Command1_Click()
Const n = 5
Dim i As Integer, j As Integer, arr(1 To n, 1 To n) As Long, a, b, c, d
For i = LBound(arr, 1) To UBound(arr, 1)
For j = LBound(arr, 2) To UBound(arr, 2)
arr(i, j) = 1
a = a + arr(i, j)
If i = j Then b = b + arr(i, j)
If j = UBound(arr, 1) - i + 1 Then b = b + 1
If i = LBound(arr, 1) Or i = UBound(arr, 1) Or j = LBound(arr, 2) Or j = UBound(arr, 2) Then c = c + 1
Next
Next
MsgBox "总计:" & a & vbNewLine & "2条对角线之和:" & b & vbNewLine & "4周数字之和" & c
End Sub
全部回答
- 1楼网友:duile
- 2021-07-20 14:57
Private Sub Command1_Click()
Dim a(0 To 4, 0 To 4)
For i = 0 To 4
For j = 0 To i
a(i, j) = Val(InputBox("", "输入数组的值"))
s = s + a(i, j)
If i = j Then
s1 = s1 + a(i, j)
End If
s2 = a(0, 4) + a(1, 3) + a(2, 2) + a(4, 0)
Next j
s3 = a(0, i) + a(i, 0) + a(i, 4) + a(4, i)
Next i
Print s, s1, s2, s3
End Sub
看,是不是你要的??
- 2楼网友:酒醒三更
- 2021-07-20 13:30
Private Sub Command1_Click()
Dim a(5, 5) As Integer
Dim i, j As Integer, sum As Integer, b_sum As Integer, x_sum As Integer
For i = 1 To 5
For j = 1 To 5
a(i, j) = 1
sum = sum + a(i, j)
If i = j Or i + j = 5 Then x_sum = x_sum + a(i, j)
If i = 1 Or i = 5 Or j = 1 Or j = 5 Then b_sum = b_sum + a(i, j)
Next j
Next i
Print "总和是:" & sum
Print "四周的和是:" & b_sum
Print "两条对角线和是:" & x_sum
End Sub
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯