English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
所有分類

假如有5個數字,1,2,3,4,5,隨機排列,但2一定要排在5之前,要怎麼用vb寫出來,麻煩各位厲害的大大敎敎我吧!

2006-04-10 16:08:30 · 2 個解答 · 發問者 阿俊 1 in 電腦與網際網路 程式設計

2 個解答

Private Sub Command1_Click()Dim S$, N$RandomizeDo  Do    N = Int(Rnd * 5 + 1)  Loop While InStr(S, N) Or (N = 2 And InStr(S, 5) = 0)  S = N & SLoop Until Len(S) = 5Print SEnd Sub

2006-04-12 06:48:43 · answer #1 · answered by W.J.S. 7 · 0 0

因為我這台電腦沒有灌VB
所以我用office內建的VBA來寫
程式碼如下
Dim a(4)

Private Sub UserForm_Click()
Randomize
a(0) = 1
a(1) = 3
a(2) = 4
For i = 0 To 4
b = Int(Rnd * 2)
a(3) = a(b)
a(b) = a(2)
a(2) = a(3)
Next
b = Int(Rnd * 4)
a(3) = a(b)
a(b) = 2
b = 4 - Int(Rnd * (4 - b))
a(4) = a(b)
a(b) = 5
Label1 = ""
For i = 0 To 4
Label1 = Label1 & a(i)
Next
End Sub

2006-04-12 01:42:00 · answer #2 · answered by ? 2 · 0 0

fedest.com, questions and answers