大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
象山网站建设公司创新互联公司,象山网站设计制作,有大型网站制作公司丰富经验。已为象山超过千家提供企业网站建设服务。企业网站搭建\成都外贸网站建设公司要多少钱,请找那个售后服务好的象山做网站的公司定做!
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点虐 2008
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
你是不是应该对最大值和最小值赋初值(比如把 r(1) 赋给最大值和最小值)呢?不然最小值默认初始值是‘0’,后面的判断就不起作用了。你可以加个断点试试,他们的初始值是多少。。。
'产生10个正整数
Dim intArr(10) As Integer
For i As Integer = 1 To 10
intArr(i - 1) = Int(Rnd() * 99) + 1
Next
'取最小值
Dim intMin As Integer = intArr(0)
For j As Integer = 1 To UBound(intArr) - 1
If intMin intArr(j) Then intMin = intArr(j)
Next
'取最大值
Dim intMax As Integer = intArr(0)
For j As Integer = 1 To UBound(intArr) - 1
If intMax intArr(j) Then intMax = intArr(j)
Next
Dim strTemp As String = ""
For tempi As Integer = 0 To UBound(intArr) - 1
strTemp = strTemp intArr(tempi) ","
Next
MessageBox.Show(strTemp ControlChars.CrLf "min: " intMin.ToString ControlChars.CrLf "max: " intMax.ToString)