制作一个宾果游戏,每个人都有相同的号码

时间:2017-10-30 00:04:31

标签: vb.net list random

我正在制作一个程序,可以为100个人创建宾果卡,并为他们提供所有不同的号码。但是刚才我的代码给了每个人完全相同的15个数字。任何帮助将不胜感激,谢谢。

  Structure Number
        Dim number As Integer
    End Structure
    Structure Player
        Dim name As String
        Dim numbers() As Number
        Dim numbers_left As Integer
    End Structure
    Dim players As New List(Of Player)
    Dim selectednumber As Integer
    Dim used As New List(Of Integer)
    Dim random As New Random
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim Number As Number
        Dim player As Player
        ReDim player.numbers(14)

        For i = 1 To 100
            For j = 0 To 14
SelectNumber:   Number.number = random.Next(1, 101)
                If player.numbers IsNot Nothing Then
                    For Each item In player.numbers
                        If item.number = Number.number Then
                            GoTo SelectNumber
                        End If
                    Next
                End If
                player.numbers(j).number = Number.number
            Next
            player.name = ("Bill" & i)
            player.numbers_left = 15
            players.Add(player)
        Next

    End Sub

1 个答案:

答案 0 :(得分:0)

我承认我并没有完全遵循所有逻辑,但看起来你需要在每个循环中重新初始化播放器,因为我认为你总是在测试第一个播放器。

Structure Number
        Dim number As Integer
    End Structure
    Structure Player
        Dim name As String
        Dim numbers() As Number
        Dim numbers_left As Integer
    End Structure
    Dim players As New List(Of Player)
    Dim selectednumber As Integer
    Dim used As New List(Of Integer)
    Dim random As New Random
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        For i = 1 To 100
            Dim Number As Number
            Dim player As Player
            ReDim player.numbers(14)
            For j = 0 To 14
SelectNumber:   Number.number = random.Next(1, 101)
                If Player.numbers IsNot Nothing Then
                    For Each item In Player.numbers
                        If item.number = Number.number Then
                            GoTo SelectNumber
                        End If
                    Next
                End If
                Player.numbers(j).number = Number.number
            Next
            Player.name = ("Bill" & i)
            Player.numbers_left = 15
            players.Add(Player)
        Next

    End Sub