vb编程 急~~!
在7个text中填入7组数,Command1_Click去掉最大最小值后求总和,Command2_Click去掉最大最小值求平均。请写出程序,谢谢
在7个text中填入7组数,Command1_Click去掉最大最小值后求总和,Command2_Click去掉最大最小值求平均。请写出程序,谢谢
答案:Dim i%, min%, max%, sum%答案补充
Private Sub Command3_Click() '随机生成7个1-100内的整数
Randomize '使每次随机数不一样
For i = 0 To 6
Text1(i).Text = Int(Rnd() * 100 + 1) '1-100数之内的整数
Next
For i = 0 To 5
min = IIf(Text1(i).Text > Text1(i + 1).Text, Text1(i + 1), Text1(i)) '得到最小值
max = IIf(Text1(i).Text > Text1(i + 1).Text, Text1(i), Text1(i + 1)) '得到最大值
Next
For i = 0 To 6
sum = sum + Val(Text1(i).Text) '7个数的总和
Next
sum = sum - min - max '减去最大最小后的总和
End Sub
Private Sub Form_Load()
Command3_Click
Text2.Text = ""
Text3.Text = ""
End Sub
Private Sub Command1_Click() '得到总和
Text2.Text = sum
End Sub
Private Sub Command2_Click() '得到平均数
Text3.Text = sum / 5
End Sub因为考虑到了你7个数输入太过繁琐.所以就多添加一个按钮Command3来实现随机生成7个数.
另外7个数的控件为数组控件text1(0)-text1(6).你只要添加text1然后选中text1按ctrl+c.然后ctrl+v就会提示是否使用数组了.选中是.继续复制到7个为止.代码比堕落天使的要好的多.还有疑问可以联系我Private Sub Command1_Click()
Dim min As Long
Dim max As Long
Dim sum As Long
Dim n As Integer
For n = 0 To 6 Step 1
If n = 0 Then min = Val(Text1(0).Text)
If n = 0 Then max = Val(Text1(0).Text)
If Val(Text1(n).Text) < min Then min = Val(Text1(n).Text)
If Val(Text1(n).Text) > max Then max = Val(Text1(n).Text)
sum = sum + Val(Text1(n).Text)
Next n
sum = sum - max - min
MsgBox sum
End Sub
Private Sub Command2_Click()
Dim min As Long
Dim max As Long
Dim sum As Long
Dim n As Integer
For n = 0 To 6 Step 1
If n = 0 Then min = Val(Text1(0).Text)
If n = 0 Then max = Val(Text1(0).Text)
If Val(Text1(n).Text) < min Then min = Val(Text1(n).Text)
If Val(Text1(n).Text) > max Then max = Val(Text1(n).Text)
sum = sum + Val(Text1(n).Text)
Next n
sum = (sum - max - min) / 5
MsgBox sum
End Sub
上一个:VB编程题目。。。。
下一个:VB 编程求解