加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

十个随机数排列(vb代码)

发布时间:2020-12-17 08:19:33 所属栏目:百科 来源:网络整理
导读:在VB中,通过编写程序代码,找出最大的数和最小的数很简单,三个数比较大小也很简单,前今天碰到了一个问题,就是,用VB制作一个小程序,实现十个随机数的排列。 我查了一些资料,在网上找到了有几个很有趣的排列视频。叫做《舞动的排序算法》。看起来很有趣

在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 代码有很多种,这只是其中的一小部分,希望对您有用所帮助。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读