在VB中,通过编写程序代码,找出最大的数和最小的数很简单,三个数比较大小也很简单,前今天碰到了一个问题,就是,用VB制作一个小程序,实现十个随机数的排列。
我查了一些资料,在网上找到了有几个很有趣的排列视频。叫做《舞动的排序算法》。看起来很有趣,也很形象,对这个问题很有帮助。地址如下:http://v.youku.com/v_show/id_XMzMyODk4NTQ4.html
http://v.youku.com/v_show/id_XMzMyODk5Njg4.html
http://v.youku.com/v_show/id_XMzMyOTAyMzQ0.html
具体的方法如下:
打开VB6.0,在Form1中添加两个按钮Command1和Command2,将其Caption属性分别改为“产生十个随机数”和“排序”,添加一个Label控件在代码窗口中输入如下代码:
Option Explicit
Dim c As Integer
Dim t As Integer
Dim flag As Integer
Dim a(9)
Dim p As String
Private Sub Command1_Click()
Dim i As Integer
p = ""
Label1.Caption = ""
a(0) = Int(100 * Rnd + 1)
For i = 1 To 9
c = Int(100 * Rnd + 1)
flag = 1
For t = 0 To i - 1
If c = a(t) Then
i = i - 1
flag = 0
Exit For
End If
Next t
If flag = 1 Then
a(i) = c
End If
Next i
For i = 0 To 9
p = p & " " & a(i)
Next i
Label1.Caption = p
End Sub
Private Sub Command2_Click()
p = ""
Dim j As Integer
Dim i As Integer
Dim temp As Integer
For i = 0 To 9
p = p & " " & a(i)
Next i
Label1.Caption = p
End Sub
第二种代码:
Option Explicit
Dim c As Integer Dim t As Integer Dim flag As Integer Dim a(9) Dim p As String Private Sub Command1_Click() Dim i As Integer p = "" Label1.Caption = "" a(0) = Int(100 * Rnd + 1) For i = 1 To 9 c = Int(100 * Rnd + 1) flag = 1 For t = 0 To i - 1 If c = a(t) Then i = i - 1 flag = 0 Exit For End If Next t If flag = 1 Then a(i) = c End If Next i For i = 0 To 9 p = p & " " & a(i) Next i Label1.Caption = p End Sub Private Sub Command2_Click() p = "" Dim j As Integer Dim i As Integer Dim temp As Integer '----------每次选最大------- For j = 0 To 9 - i If a(j) > a(j + 1) Then temp = a(j) a(j) = a(j + 1) a(j + 1) = temp End If Next j Next i For i = 0 To 9 p = p & " " & a(i) Next i Label1.Caption = p End Sub 代码有很多种,这只是其中的一小部分,希望对您有用所帮助。
原文链接:https://www.f2er.com/vb/259188.html