# 简单问题 ~~请教大虾！

hnzhc 2004-09-01 09:27:54

...全文
56 点赞 收藏 6

6 条回复

51365133 2004-09-01

Dim a(50), b(100)
Private Sub Command1_Click()
List1.Clear
For i = 1 To 100
b(i) = i
Next i
For i = 0 To 50
j = Int(Rnd * 99) + 1
If b(j) <> 0 Then
a(i) = b(j)
b(j) = 0
Else
i = i - 1
End If
Next i
For i = 0 To 50
For j = i To 50
If a(i) < a(j) Then
m = a(i): a(i) = a(j): a(j) = m
End If
Next j
Next i
For i = 0 To 50
Next i
End Sub

51365133 2004-09-01
Dim a(4)
Private Sub Command1_Click()
List1.Clear
Rnd Timer
For i = 0 To 4
a(i) = Int(Rnd * 99) + 1
Next i
For i = 0 To 4
For j = i To 4
If a(i) < a(j) Then
m = a(i): a(i) = a(j): a(j) = m
End If
Next j
Next i
For i = 0 To 4
Next i
End Sub

starsoulxp 2004-09-01

starsoulxp 2004-09-01
Option Explicit
Dim i As Integer
Dim j As Integer
Dim a(1 To 5) As Integer

Private Sub RndSelect()

Randomize
For i = 1 To 5
rLoop: a(i) = Rnd * 9 + 1
If i > 1 Then
For j = 1 To i - 1
If a(i) = a(j) Then
GoTo rLoop
End If
Next j
End If
Next i
End Sub

Private Sub Command1_Click()
Dim s As String
Call RndSelect
For i = 1 To 5
s = s & a(i) & IIf(i = 5, "", ",")
Next
Text1.Text = "选出的5个随机数为" & s

Call DescendSort
s = ""
For i = 1 To 5
s = s & a(i) & IIf(i = 5, "", ",")
Next i

Text1.Text = Text1.Text & vbCrLf & "从大到小排序：" & s

End Sub
Private Sub DescendSort()
Dim temp As Integer
i = 1
j = 1
While (i < 6)
j = i + 1
While (j < 6)
If a(i) < a(j) Then
temp = a(i)
a(i) = a(j)
a(j) = temp
End If
j = j + 1
Wend
i = i + 1
Wend

End Sub

jordi2014 2004-09-01
Randomize 语句

Randomize [number]

Randomize 用 number 将 Rnd 函数的随机数生成器初始化，该随机数生成器给 number 一个新的种子值。如果省略 number，则用系统计时器返回的值作为新的种子值。

Rnd 函数

Rnd[(number)]

Rnd 函数返回小于 1 但大于或等于 0 的值。

number 的值决定了 Rnd 生成随机数的方式。

Int((upperbound - lowerbound + 1) * Rnd + lowerbound)

jordi2014 2004-09-01

VB基础类

7476

VB 基础类

2004-09-01 09:27