vb:输入10个整数求平均值,并求出大于、等于、小于平均值的数的个数(使用选择结构、循环结构、数组)。

不会编啊,高手帮我下
2024-12-18 23:27:43
推荐回答(2个)
回答1:

Private Sub Command1_Click()
Dim a(1 To 10) As Integer '定义10个数组
Dim aa(1 To 9) As Integer '定义9个空格数组
Dim n As Integer '定义空格数
Dim s As Integer '数组和
Form1.Cls '清除print
s = 0
n = 0
For i = 1 To Len(Text1) '从text1第1位开始数到最后位
If Mid(Text1, i, 1) = " " Then '使用空格判断整数个数
n = n + 1
If n < 10 Then
aa(n) = i 'aa(n)储存空格所在位数
If n = 1 Then
a(n) = Mid(Text1, 1, aa(n) - 1) '第一个整数
Else
a(n) = Mid(Mid(Text1, 1, i - 1), aa(n - 1) + 1) '第2到9个整数
End If
Else
If n > 9 Then
Exit For '退出循环
End If
End If
If Mid(Text1, i + 1, 1) = " " Then '超过2个空格,则输入格式不对。
MsgBox "第" & i & "位输入格式不对,数字间只能一个空格!"
Exit For
End If
End If
Next i
If n = 9 Then '当存在9个空格时
a(10) = Mid(Text1, aa(9) + 1) '第10个数组
For i = 1 To 10
s = s + a(i)
Next i
For i = 1 To 10
If a(i) > s / 10 Then
j = j + 1
ElseIf a(i) = s / 10 Then
k = k + 1
ElseIf a(i) < s / 10 Then
m = m + 1
End If
Next i
Print "大于平均数的有:" & j & "个"
Print "等于平均数的有:" & k & "个"
Print "小于平均数的有:" & m & "个"
Else
MsgBox "请输入10个整数。" '不存在9个空格时 表示没有10个数
End If
End Sub

回答2:

Dim a(1 To 10) As Integer
Dim t As Integer
Dim dayu As Integer, dengyu As Integer, xiaoyu As Integer
Private Sub Command1_Click()
For i = 1 To 10
a(i) = InputBox("第" & i & "次输入", "请输入")
t = t + a(i)
Next i
For j = 1 To 10
If a(j) > t / 10 Then
dayu = dayu + 1
ElseIf a(j) = t / 10 Then
dengyu = dengyu + 1
Else
xiaoyu = xiaoyu + 1
End If
Next j
Print "平均数是:" & t / 10
Print "大于平均数的值有" & dayu & "个"
Print "等于平均数的值有" & dengyu & "个"
Print "小于平均数的值有" & xiaoyu & "个"
End Sub