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

1个回答

  • 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 '清除prints = 0n = 0For i = 1 To Len(Text1) '从text1第1位开始数到最后位

    If Mid(Text1,i,1) = " " Then '使用空格判断整数个数n = n + 1If n < 10 Then

    aa(n) = i 'aa(n)储存空格所在位数

    If n = 1 Then

    a(n) = Mid(Text1,1,aa(n) - 1) '第一个整数Elsea(n) = Mid(Mid(Text1,1,i - 1),aa(n - 1) + 1) '第2到9个整数End IfElseIf n 9 Then

    Exit For '退出循环End IfEnd IfIf Mid(Text1,i + 1,1) = " " Then '超过2个空格,则输入格式不对.

    MsgBox "第" & i & "位输入格式不对,数字间只能一个空格!"Exit ForEnd IfEnd IfNext iIf n = 9 Then '当存在9个空格时

    a(10) = Mid(Text1,aa(9) + 1) '第10个数组

    For i = 1 To 10

    s = s + a(i)Next iFor i = 1 To 10

    If a(i) s / 10 Thenj = j + 1ElseIf a(i) = s / 10 Thenk = k + 1ElseIf a(i) < s / 10 Thenm = m + 1End IfNext iPrint "大于平均数的有:" & j & "个"

    Print "等于平均数的有:" & k & "个"

    Print "小于平均数的有:" & m & "个"ElseMsgBox "请输入10个整数." '不存在9个空格时 表示没有10个数End IfEnd Su