【问题标题】:How to use Randomize to choose from array list如何使用 Randomize 从数组列表中进行选择
【发布时间】:2014-11-12 12:42:50
【问题描述】:

我正在尝试根据 3 个数组列表随机生成图片框(stickimage)将出现的 3 个不同位置。

到目前为止我的代码:

    Private Sub GenerateObjects()
    Dim RandomClass As New Random()         'Generate random number
    Dim Y As Integer                        'Y axis
    Dim ObstaclePos(3) As Integer           'Position where obstacle is allocated
    ObstaclePos(1) = 404
    ObstaclePos(2) = 310
    ObstaclePos(3) = 290

    Me.stickImage.Left -= 20


    If stickImage.Bounds.IntersectsWith(LeftStickBrake.Bounds) Then
        For pos = 1 To 3
            Y =                                             'This is where I am stuck
            stickImage.Location = New Point(1014, Y)


        Next pos
    End If
End Sub

【问题讨论】:

  • Y = ObstaclePos(RandomClass.Next(1, 4))

标签: .net arrays vb.net random basic


【解决方案1】:

首先,检查您的数组声明。

Dim ObstaclePos(3) As Integer
ObstaclePos(1) = 404
ObstaclePos(2) = 310
ObstaclePos(3) = 290

你应该从索引0开始。

Dim ObstaclePos(2) As Integer
ObstaclePos(0) = 404
ObstaclePos(1) = 310
ObstaclePos(2) = 290

现在您可以使用Random.Next(Integer) 生成随机索引。

Dim Y As Integer = ObstaclePos(RandomClass.Next(ObstaclePos.Length))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    相关资源
    最近更新 更多