VB中如何求最大值和最小值,我写了下面的代码可是只能求最大值和平均值。最小值一直显示0
PrivateSubCommand1_Click()DimNumAsInteger,MaxAsInteger,MinAsInteger,sAsIntegerFori=1T...
Private Sub Command1_Click()
Dim Num As Integer, Max As Integer, Min As Integer, s As Integer
For i = 1 To 20
Num = Int(150 * Rnd + 50)
Print Num;
If Max < Num Then Max = Num
If Min > Num Then Min = Num
s = s + Num
Next i
Print "最大值:"; Max
Print "最小值:"; Min; "平均值:"; s / 20
End Sub 展开
Dim Num As Integer, Max As Integer, Min As Integer, s As Integer
For i = 1 To 20
Num = Int(150 * Rnd + 50)
Print Num;
If Max < Num Then Max = Num
If Min > Num Then Min = Num
s = s + Num
Next i
Print "最大值:"; Max
Print "最小值:"; Min; "平均值:"; s / 20
End Sub 展开
展开全部
原因:代码不正确造成的。
1、首先打开需要编辑的Excel表格,进入到编辑页面中。
2、然后在编辑的窗口中,鼠标右键单击工作表,选择打开“查看代码”。
3、然后在弹出来的窗口中点击输入下方的代码:
Private Sub Command1_Click()
Dim Num As Integer, Max As Integer, Min As Integer, s As Integer
For i = 1 To 20
Num = Int(150 * Rnd + 50)
Print Num;
If i = 1 Then Max = Num: Min = Num '第一次循环时,先对最大最小值赋初值
If Max < Num Then Max = Num
If Min > Num Then Min = Num
s = s + Num
Next i
Print '换行
Print "最大值:"; Max;
Print "最小值:"; Min; "平均值:"; s / 20
End Sub4、然后点击工具栏中的“运行”图标。
5、然后就完成了。
展开全部
Private Sub Command1_Click()
Dim Num As Integer, Max As Integer, Min As Integer, s As Integer
Min = 999
For i = 1 To 20
Num = Int(150 * Rnd + 50)
Print Num;
If Max < Num Then Max = Num
If Min > Num Then Min = Num
s = s + Num
Next i
Print "最大值:"; Max
Print "最小值:"; Min; "平均值:"; s / 20
End Sub
原因是你的Min的初始值是0,那么产生的任何一个随机数都比它大,那么它的值就一直不变。只要先给它赋值一个足够大的数即可
Dim Num As Integer, Max As Integer, Min As Integer, s As Integer
Min = 999
For i = 1 To 20
Num = Int(150 * Rnd + 50)
Print Num;
If Max < Num Then Max = Num
If Min > Num Then Min = Num
s = s + Num
Next i
Print "最大值:"; Max
Print "最小值:"; Min; "平均值:"; s / 20
End Sub
原因是你的Min的初始值是0,那么产生的任何一个随机数都比它大,那么它的值就一直不变。只要先给它赋值一个足够大的数即可
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Private Sub Command1_Click()
Dim Num As Integer, Max As Integer, Min As Integer, s As Integer
For i = 1 To 20
Num = Int(150 * Rnd + 50)
Print Num;
If i = 1 Then Max = Num: Min = Num '第一次循环时,先对最大最小值赋初值
If Max < Num Then Max = Num
If Min > Num Then Min = Num
s = s + Num
Next i
Print '换行
Print "最大值:"; Max;
Print "最小值:"; Min; "平均值:"; s / 20
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询