文本狂里随即出现20个0-100之间的数字 点击按钮 首先将20个数字排序,再求出最大值和下表、20个数字的和、平均值 就一个按钮 看谁写的简单写。 要最简单哦。
VB写一个简单的程序
- 提问者网友:鐵馬踏冰河
- 2021-04-15 13:25
- 五星知识达人网友:我住北渡口
- 2021-04-15 13:56
Private Sub Command1_Click()
Dim maxValue As Integer
Dim minValue As Integer
Dim sumValue As Integer
Dim avgValue As Integer
Dim i As Integer
Dim j As Integer
Dim tmpValue As Integer
Dim tmpArr() As String
Dim sw As Integer
Text1.Text = ""
For i = 1 To 20
Randomize 5
tmpValue = Int((100 + 1) * Rnd)
sumValue = sumValue + tmpValue
Text1.Text = Text1.Text & Int((100 + 1) * Rnd) & " "
Next i
avgValue = sumValue / 20
tmpArr = Split(Text1.Text, " ")
For i = 0 To 19
tmpValue = tmpArr(i)
For j = i + 1 To 19
If tmpValue > tmpArr(j) Then
tmpArr(i) = tmpArr(j)
tmpArr(j) = tmpValue
End If
Next j
Next i
minValue = tmpArr(0)
maxValue = tmpArr(19)
Text1.Text = ""
For i = 0 To 19
Text1.Text = Text1.Text & tmpArr(i) & " "
Next i
Print "最小值:" & minValue
Print "最大值:" & maxValue
Print "平均值:" & avgValue
Print "总 和:" & sumValue
End Sub
- 1楼网友:想偏头吻你
- 2021-04-15 15:07
测试过,绝对可用 Private Sub Command1_Click() Dim i As Integer Dim j As Integer Dim k As Integer Dim a() Dim b, c As Integer Dim max As Integer Dim min As Integer Dim sum As Integer Dim doucount As Integer k = InputBox("请输入N的值", "输入", "")
ReDim a(k) For i = 0 To k - 1 a(i) = Int(Rnd * 100) Next i For i = 0 To k - 1 b = i For j = i + 1 To k - 1 If a(b) < a(j) Then b = j Next j c = a(b) a(b) = a(i) a(i) = c
'Print a(i); Next i For i = 0 To i - 1 sum = sum + a(i) If a(i) Mod 2 <> 0 Then doucount = doucount + 1 End If Next Print "最大值"; a(0); "最小值 " & a(i); "总和 " & sum; " 平均值 " & sun / i; " 偶数个数 " & doucount End Sub