zoukankan      html  css  js  c++  java
  • 十个随机数排列(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

    代码有很多种,这只是其中的一小部分,希望对您有用所帮助。

        

      

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    python---常见排序算法
    flask之session
    Python常考面试题
    MySQL一致性非锁定读原理以及MVCC简介
    mysql面试常考知识点
    数据库学习笔记4数据系统的组成
    工作记录之拯救rm -rf /*(无root权限拯救恢复基础功能)
    数据库学习笔记3数据库的系统结构
    数据库学习笔记2数据模型
    数据库学习笔记1
  • 原文地址:https://www.cnblogs.com/lucari/p/4608609.html
Copyright © 2011-2022 走看看