多个变量的Visual Basic随机数

时间:2017-09-08 13:21:44

标签: vb.net visual-studio

目标:制作游戏的用户将从16种不同的选项中进行选择,并获得16个独特的回复。这些响应是用户培训的简单安全提示。

它们必须是随机顺序,因此没有人可以屏幕显示并预测哪个按钮产生哪个结果,并且在单个游戏中它们无法重复使用/重复响应。

我一直在来回使用数组或选择案例。选择案例看起来更有希望,但我不确定如何随机分配16个不同的按钮来提供正确的不重复响应。

请求的代码很差,

        'Create Select Case random value and assign to buttons
    Do
        btn2 = CInt(Int((3 * Rnd()) + 1))
        'lblDescript so I can see what btn2 is outputting
        lblDescript.Text = btn2.ToString
    Loop While btn2 <> btn1 Or btn3
 '***************************************************************************************
'Game Buttons 2 - 16 ************************************************************
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    'Button Style
    Button2.ForeColor = Color.Transparent
    Button2.BackColor = Color.Transparent
    Button2.FlatStyle = FlatStyle.Flat
    Button2.Text = ""
    Button2.UseVisualStyleBackColor = False
    'Button2.Enabled = False

    'Select Case
    Select Case btn2
        Case Is = 1
            PictureBox1.Image = My.Resources.Login
            Button2.BackgroundImage = My.Resources.Login
            lblPicName.Text = "Two - Factor Authentication"
            lblDescript.Text = "Two - Factor Authentication, or 2FA adds an extra step to a basic log-in procedure."
        Case Is = 2
            'Content PaceHolder*****
            PictureBox1.Image = My.Resources.Cloud
            Button2.BackgroundImage = My.Resources.Cloud
            lblPicName.Text = "Two - Factor Authentication"
            lblDescript.Text = "Two - Factor Authentication, or 2FA adds an extra step to a basic log-in procedure."
        Case Is = 3
            'Content PaceHolder*****
            PictureBox1.Image = My.Resources.Pwmanager
            Button2.BackgroundImage = My.Resources.Pwmanager
            lblPicName.Text = "Two - Factor Authentication"
            lblDescript.Text = "Two - Factor Authentication, or 2FA adds an extra step to a basic log-in procedure."
        Case Is = 4
    End Select

我提供的图片显示了当我选择应用程序中的第一个按钮时我需要做什么。

图片说明:

enter image description here

用户选择按钮后,照片将显示为按钮,右侧显示名称和说明。

2 个答案:

答案 0 :(得分:1)

我认为你应该寻求一种更基于数据而不是代码的解决方案。您的代码似乎很难连接大量相似或重复的逻辑,而您可以将所有可能性存储在数据结构中并随机播放。然后,您只能使用代码来显示随机播放的结果。

Public Class Form1

   Class SecurityTip
      Public PicName As String
      Public Description As String
      Public Image As Image
   End Class

   Private TipList As SecurityTip()
   Private ButtonArray As Button()

   Protected Overrides Sub OnLoad(e As EventArgs)
      MyBase.OnLoad(e)
      ButtonArray = New Button() {
         Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8,
         Button9, Button10, Button11, Button12, Button13, Button14, Button15, Button16}
      For I As Integer = 0 To ButtonArray.Length - 1
         AddHandler ButtonArray(I).Click, AddressOf Button_Click
      Next
      InitializeTips()
      ShuffleTips()
   End Sub

   Public Sub InitializeTips()
      TipList = New SecurityTip() {
         New SecurityTip() With {
            .Image = My.Resources.Login,
            .PicName = "Two - Factor Authentication",
            .Description = "Two - Factor Authentication, or 2FA adds an extra step to a basic log-in procedure."},
         New SecurityTip() With {
            .Image = My.Resources.Cloud,
            .PicName = "Cloud Authentication",
            .Description = "Cloud authentication might also be referred to as federated identity or something."},
         New SecurityTip() With {
            .Image = My.Resources.Pwmanager,
            .PicName = "Password Manager",
            .Description = "Password managers help users maintain separate passwords for different sites."}
         }
   End Sub

   Public Sub ShuffleTips()
      Dim R As New Random()
      For i As Integer = 0 To TipList.Length - 1
         Dim SwapIndex = R.Next(TipList.Length)
         Dim Temp = TipList(SwapIndex)
         TipList(SwapIndex) = TipList(i)
         TipList(i) = Temp
      Next
   End Sub

   Public Sub PresentTip(Index As Integer)
      With TipList(Index)
         PictureBox1.Image = .Image
         lblPicName.Text = .PicName
         lblDescript.Text = .Description
         ButtonArray(Index).BackgroundImage = TipList(Index).Image
      End With
   End Sub

   Private Sub Button_Click(sender As Object, e As EventArgs)
      For I As Integer = 0 To ButtonArray.Length - 1
         If ButtonArray(I) Is sender Then
            PresentTip(I)
            Exit Sub
         End If
      Next
   End Sub
End Class

答案 1 :(得分:0)

在您的代码中,

下面
'Button2.Enabled = False

添加

Dim rndNum As Random
Dim randomChoice As Integer = rndNum.Next(1, 4)

和你的选择案例陈述

Select Case randomChoice
    Case 1
    Case 2
    Case 3
    Case 4

包括每个课程的代码。