⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.vb

📁 C#随机小软件,随机数无重复,数组大小,维数可设置.
💻 VB
字号:
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "

    Public Sub New()
        MyBase.New()

        '该调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        '在 InitializeComponent() 调用之后添加任何初始化

    End Sub

    '窗体重写 dispose 以清理组件列表。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer

    '注意: 以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改此过程。
    '不要使用代码编辑器修改它。
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
    Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
    Friend WithEvents Button2 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
        Me.Label1 = New System.Windows.Forms.Label
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.Label2 = New System.Windows.Forms.Label
        Me.TextBox2 = New System.Windows.Forms.TextBox
        Me.ListBox1 = New System.Windows.Forms.ListBox
        Me.Button2 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.BackColor = System.Drawing.Color.Transparent
        Me.Label1.Font = New System.Drawing.Font("新宋体", 10.5!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
        Me.Label1.ForeColor = System.Drawing.Color.LightSalmon
        Me.Label1.Location = New System.Drawing.Point(0, 8)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(88, 23)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "输入总人数"
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(104, 8)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.TabIndex = 1
        Me.TextBox1.Text = ""
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(208, 8)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 2
        Me.Button1.Text = "确定"
        '
        'Label2
        '
        Me.Label2.BackColor = System.Drawing.Color.Transparent
        Me.Label2.Font = New System.Drawing.Font("新宋体", 10.5!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
        Me.Label2.ForeColor = System.Drawing.Color.LightSalmon
        Me.Label2.Location = New System.Drawing.Point(0, 48)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(72, 23)
        Me.Label2.TabIndex = 4
        Me.Label2.Text = "数组大小"
        '
        'TextBox2
        '
        Me.TextBox2.Location = New System.Drawing.Point(104, 48)
        Me.TextBox2.Name = "TextBox2"
        Me.TextBox2.TabIndex = 5
        Me.TextBox2.Text = ""
        '
        'ListBox1
        '
        Me.ListBox1.BackColor = System.Drawing.Color.CornflowerBlue
        Me.ListBox1.Cursor = System.Windows.Forms.Cursors.No
        Me.ListBox1.Font = New System.Drawing.Font("新宋体", 10.5!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
        Me.ListBox1.ForeColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(255, Byte), CType(128, Byte))
        Me.ListBox1.ItemHeight = 14
        Me.ListBox1.Location = New System.Drawing.Point(0, 80)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.Size = New System.Drawing.Size(288, 256)
        Me.ListBox1.TabIndex = 6
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(208, 48)
        Me.Button2.Name = "Button2"
        Me.Button2.TabIndex = 7
        Me.Button2.Text = "清除"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.BackgroundImage = CType(resources.GetObject("$this.BackgroundImage"), System.Drawing.Image)
        Me.ClientSize = New System.Drawing.Size(288, 342)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.ListBox1)
        Me.Controls.Add(Me.TextBox2)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.Label1)
        Me.Font = New System.Drawing.Font("新宋体", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
        Me.ForeColor = System.Drawing.Color.Black
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim renshu As Integer '总人数
        renshu = Me.TextBox1.Text '取得文本框的值

        Dim daxiao As Integer '组的大小
        daxiao = TextBox2.Text '取得文本框的值

        Dim zushu As Integer '组数
        Dim temp As Integer = renshu Mod daxiao '判断每组是否完整
        If temp = 0 Then
            zushu = renshu / daxiao     '完整
        Else : zushu = renshu \ daxiao + 1 '不完整 +1
        End If

        Dim con As Integer  '记数
        con = 0


        Dim tmp(renshu) As Integer  '数组 存放所有的随机数
        Dim MyValue As String '变量 存放产生的单个随机数

        Dim mingzi() As String = New String() {"涂作为", "邓小兰", "黄  叶", "赵建国", "皮志宇", "王  鹏", "王  剑", "刘  杰", "段沙慧", "李水源", "曾华臣", "陈  更", "赵孝天", "张  杰", "蒋友邻", "吴尚文", "杨方林", "伍  辉", "黄  碧", "伍  晨", "朱旭鹏", "罗立娟", "张  迎", "杨  鑫", "李  礼", "胡典武", "朱崇华", "戴  丹", "王  波", "周  彬", "谭宇阳", "罗  捷", "张  扬", "肖  姣", "肖  芬", "贺艳荣", "龚  斌", "隋  念", "夏莉莉", "龙景贵", "彭向前 ", "奉先壁", "冯立芳", "李  年", "张  岱", "杨敏姣", "许剑平", "资  辉", "李文超", "陈智亚", "罗  敏", "刘  洋"}

        Randomize() '初始化随机数生成器

        Dim str As String
        str = New String("-----------") '组别分隔符

        Dim a, c As Integer
        For a = 1 To zushu
            
            Me.ListBox1.Items.Add(str) '加入列表框

            Dim x As Integer '判断数组的大小
            Dim temp1 As String '临时变量 用于存放tmp数组值
            Dim y As Integer
            y = 0
           
            For x = 1 To daxiao
                MyValue = CInt(Int((renshu * Rnd()) + 1)) '产生随机数
                ' MsgBox("rrrrr" & MyValue)
                '判断是否有重复
                For y = 0 To con
                    temp1 = tmp(y)
                    If MyValue = temp1 Then
                        x = x - 1 '将当前循环值取消 重做循环
                        c = 1
                        Exit For
                    Else
                        c = 0
                    End If
                Next
                '处理判断结果
                If c = 0 Then
                    tmp(con) = MyValue '把结果写入数组
                    Dim i As Integer
                    i = MyValue - 1
                    'Me.ListBox1.Items.Add(tmp(con)) '把结果写入列表框
                    Me.ListBox1.Items.Add(mingzi(i))

                    con = con + 1 '数组记数+1
                    If con = renshu Then
                        Me.ListBox1.Items.Add(str)
                        Exit Sub
                    End If
                Else ' MsgBox("出现重复数字。当前数组大小为" & con)

                End If
            Next

        Next

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.ListBox1.Items.Clear()
    End Sub
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -