Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
象山網(wǎng)站建設公司創(chuàng)新互聯(lián)公司,象山網(wǎng)站設計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為象山超過千家提供企業(yè)網(wǎng)站建設服務。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設公司要多少錢,請找那個售后服務好的象山做網(wǎng)站的公司定做!
Randomize() '產(chǎn)生隨機數(shù)種子 以防止每次生成結(jié)果都一樣
Dim a(100) As Integer '聲明數(shù)組存放數(shù)據(jù) 用來保存隨機數(shù)結(jié)果
Dim max As Integer, min As Integer '聲明兩個變量存最大值和最小值
min = 1000 : max = 0 '設置初值
For i = 0 To 99 '循環(huán)100次
a(i) = Int(Rnd() * 1000 + 1) '用rnd生成一個隨機數(shù) 由于rnd范圍為0-1之間的小數(shù).所以*1000設置他的范圍為0到999之間 加1變成1到1000之間
If a(i) = max Then max = a(i) '如果當前數(shù)值大于最大值的變量就保存
If a(i) = min Then min = a(i) '如果當前數(shù)值小于最小值的變量就保存
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’,后面的判斷就不起作用了。你可以加個斷點試試,他們的初始值是多少。。。
'產(chǎn)生10個正整數(shù)
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)