VB中 求平均值

2024-12-22 01:09:31
推荐回答(4个)
回答1:

假设wendu_i1数组已赋值了(至于怎么赋值自行解决)
for i=3 to 10
if wendu_i1(1) i1 =wendu_i1(1)
wendu_i1(1)=wendu_i1(i)
wendu_i1(i)=i1
end if
if wendu_i1(2)>wendu_i1(i) then
i1 =wendu_i1(2)
wendu_i1(2)=wendu_i1(i)
wendu_i1(i)=i1
end if
next
这样后wendu_i1(1)是最大值 ,wendu_i1(2)是最小值
i1=0
for i=3 to 10
i1=i1+wendu_i1(i)
next
i1=i1/8
print i1

回答2:

以下代码用到的是Text1控件数组和Command1按钮,添加控件数组的方法是先添加一个Text1,然后复制……粘贴……粘贴……直到六个文本框为止。具体代码如下:
-------------------------------------------------------------------------------------------------
Option Explicit
Dim i As Integer

Private Sub Command1_Click()
Dim k As Integer
Dim Sum As Single
Dim Avg As Single
Sum = 0
k = 0
For i = 0 To 5
If Len(Trim(Text1(i))) > 0 Then
k = k + 1 '统计输入了几个文本框
Sum = Sum + Val(Text1(i)) '求和
End If
Next
MsgBox k
If k = 0 Then MsgBox "至少要输入一个数:": Exit Sub '一个也没有输入时提示错误,然后退出过程
Avg = Sum / k '求平均值
MsgBox "平均数是:" & Avg
End Sub

Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
For Index = 0 To 5
KeyAscii = ValiText(KeyAscii, "0123456789", True, True)
Next
End Sub

'--------------------------------{ValiText}--------------------------------
'【函数说明】:
'控制(允许或限制)文本输入,一般用于文本框的KeyPress事件。
'【参数说明】:
'⑴.KeyIn →键盘输入字符的ASCII码;
'⑵.ValiStr →控制的文本;
'⑶.BackSpace →是否允许使用退格键;
'⑷.IsAllowStr →是否允许输入控制的文本。
'----------------------------------------------------------------------------
Function ValiText(KeyIn As Integer, ValiStr As String, BackSpace As Boolean, IsAllowStr As Boolean) As Integer
Dim ValiList As String
Dim KeyOut As Integer
'限制使用退格键
If BackSpace = False Then
ValiList = UCase(ValiStr) & Chr(8) 'Chr(8)是退格键
'允许使用退格键
Else
ValiList = UCase(ValiStr)
End If
'限制输入控制的文本
If IsAllowStr = False Then
If InStr(1, ValiList, UCase(Chr(KeyIn)), 1) > 0 Then
KeyOut = 0
Else
KeyOut = KeyIn
End If
'允许输入控制的文本
Else
If InStr(1, ValiList, UCase(Chr(KeyIn)), 1) > 0 Then
KeyOut = KeyIn
Else
KeyOut = 0
End If
End If
ValiText = KeyOut
End Function

回答3:

dim max,min as single
min=wendu_i1(1)
for a=1 to 10
if wendu_i1(a)>max then max=wendu_i1(a)
if wendu_il(a)i1=i1+wendu_i1(a)
next
i1=(i1-min-max)/8
print i1

回答4:

Private Sub Command1_Click()
Dim i1 As Single
Dim wendu_i1(1 To 10) As Single
Dim i As Integer, max As Single, min As Integer, m As Integer, n As Integer

max = wendu_i1(1)
min = wendu_i1(10)
For i = 1 To 10
If max < wendu_i1(i) Then max = wendu_i1(i): m = i
If min > wendu_i1(i) Then min = wendu_i1(i): n = i
Next
wendu_i1(m) = wendu_i1(1): wendu_i1(1) = max
wendu_i1(n) = wendu_i1(10): wendu_i1(10) = min
For i = 2 To 9
i1 = i1 + wendu_i1(i)
Next
i1 = i1 / 8

End Sub