Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
成都创新互联成都企业网站建设服务,提供成都网站制作、网站建设网站开发,网站定制,建网站,网站搭建,网站设计,响应式网站设计,网页设计师打造企业风格网站,提供周到的售前咨询和贴心的售后服务。欢迎咨询做网站需要多少钱:028-86922220
Randomize() '产生随机数种子 以防止每次生成结果都一样
Dim a(100) As Integer '声明数组存放数据 用来保存随机数结果
Dim max As Integer, min As Integer '声明两个变量存最大值和最小值
min = 1000 : max = 0 '设置初值
For i = 0 To 99 '循环100次
a(i) = Int(Rnd() * 1000 + 1) '用rnd生成一个随机数 由于rnd范围为0-1之间的小数.所以*1000设置他的范围为0到999之间 加1变成1到1000之间
If a(i) = max Then max = a(i) '如果当前数值大于最大值的变量就保存
If a(i) = min Then min = a(i) '如果当前数值小于最小值的变量就保存
Next
MsgBox("最大值为" max)
MsgBox("最小值为" min)
End Sub
'vb.net2008
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim min, max As Integer
Dim a() As Integer = {3, 2, 4, 1}
min = Proc(a)(0)
max = Proc(a)(1)
End Sub
Private Function Proc(ByVal a() As Integer)
Dim a_out(a.GetUpperBound(0)), a_m(1) As Integer
System.Array.Copy(a, a_out, a.GetUpperBound(0) + 1)
System.Array.Sort(a_out)
a_m(0) = a_out(0)
a_m(1) = a_out(a_out.GetUpperBound(0))
Return a_m
End Function
End Class
MaxOrMin 指示返回最大还是最小.
Private Function Math(ByVal num1 As Integer, ByVal num2 As Integer, ByVal num3 As Integer, ByVal MaxOrMin As Short) As Integer
Dim s() As Integer = {num1, num2, num3}
Dim max, min As Integer
If s(0) s(1) Then
min = s(0)
max = s(1)
End If
If s(1) s(2) Then
min = s(1)
max = s(2)
End If
If MaxOrMin = 0 Then Return max
If MaxOrMin = 1 Then Return min
End Function
你是不是应该对最大值和最小值赋初值(比如把 r(1) 赋给最大值和最小值)呢?不然最小值默认初始值是‘0’,后面的判断就不起作用了。你可以加个断点试试,他们的初始值是多少。。。