作业帮 > 综合 > 作业

用VB写一下该程序 输入一批数据,统计其正数平均值和负数平均值,输入零时停止

来源:学生作业帮 编辑:大师作文网作业帮 分类:综合作业 时间:2024/11/18 21:02:42
用VB写一下该程序 输入一批数据,统计其正数平均值和负数平均值,输入零时停止
用VB写一下该程序 输入一批数据,统计其正数平均值和负数平均值,输入零时停止
Dim num As Integer
Dim zhengshu As Single
Dim i As Integer
Dim fushu As Single
Dim j As Integer
Private Sub Command1_Click()
Do
num = Val(InputBox("请输入一个数","",""))
If num > 0 Then
zhengshu = zhengshu + num
i = i + 1
End If
If num < 0 Then
fushu = fushu + num
j = j + 1
End If
Loop Until num = 0
Text1.Text = Str(zhengshu / i)
Text2.Text = Str(fushu / j)
End Sub
Private Sub Form_Load()
Text1.Text = ""
Text2.Text = ""
zhengshu = 0
fushu = 0
i = 0
j = 0
End Sub
代码经过本人自己验证,可行
再问: 请问一下那 截尾平均数的VB代码 怎么写
再答: 截尾平均数?没明白你的意思。
再问: 输入N个数(N>=3),去掉最大的数,去掉最小数,求平均值 用VB的顺序结构,循环结构,分支结构来写
再答: 提示你一下,由于我定义数组是一个整型数组,所以你输入的时候不要输小数啊! Private Sub Form_Click() Dim n As Integer, a() As Integer, avg As Single, max As Integer, min As Integer, i As Integer, sum As Integer n = InputBox("请输入数字的个数") If n < 3 Then MsgBox "输入的数太少,请重新输入", vbExclamation + vbOKOnly, "警告" Exit Sub Else ReDim a(1 To n) As Integer For i = 1 To n a(i) = InputBox("请输入第" & i & "个数") sum = sum + a(i) Next i max = a(1) min = a(1) For i = 1 To n If max < a(i) Then max = a(i) End If If min > a(i) Then min = a(i) End If Next i sum = sum - max - min avg = sum / (n - 2) Print avg End If End Sub