Vb.net Visual Basic多变量随机数

Vb.net Visual Basic多变量随机数,vb.net,visual-studio,Vb.net,Visual Studio,目标:要制作一个游戏,用户将从16个不同选项中进行选择,并收到16个独特的响应。这些回答是用户培训的简单安全提示 它们必须以随机顺序排列,这样就没有人可以通过屏幕查看和预测哪个按钮会产生哪个结果,并且它们不能在单个游戏中重复使用/复制响应 我一直在反复使用数组或select case。select案例看起来更有希望,但是我不确定如何随机分配16个不同的按钮来提供正确的、不重复的响应 请求的代码很差 'Create Select Case random value and assi

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

它们必须以随机顺序排列,这样就没有人可以通过屏幕查看和预测哪个按钮会产生哪个结果,并且它们不能在单个游戏中重复使用/复制响应

我一直在反复使用数组或select case。select案例看起来更有希望,但是我不确定如何随机分配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
创建选择大小写随机值并分配给按钮 做 btn2=CInt(Int((3*Rnd())+1)) 'lblDescript,以便查看btn2输出的内容 lblDescript.Text=btn2.ToString 在btn2 btn1或btn3时循环 '*************************************************************************************** '游戏按钮2-16************************************************************ 私有子按钮2\u单击(发送者作为对象,e作为事件参数)处理按钮2。单击 '按钮样式 Button2.ForeColor=颜色。透明 Button2.BackColor=颜色。透明 按钮2.FlatStyle=FlatStyle.Flat 按钮2.Text=“” Button2.UseVisualStyleBackColor=False 'Button2.Enabled=False '选择案例 选择案例btn2 情况是=1 PictureBox1.Image=My.Resources.Login Button2.BackgroundImage=My.Resources.Login lblPicName.Text=“双因素身份验证” lblDescript.Text=“双因素身份验证或2FA为基本登录过程添加了一个额外步骤。” 情况是=2 “内容起搏器***** PictureBox1.Image=My.Resources.Cloud Button2.BackgroundImage=My.Resources.Cloud lblPicName.Text=“双因素身份验证” lblDescript.Text=“双因素身份验证或2FA为基本登录过程添加了一个额外步骤。” 情况是=3 “内容起搏器***** PictureBox1.Image=My.Resources.Pwmanager Button2.BackgroundImage=My.Resources.Pwmanager lblPicName.Text=“双因素身份验证” lblDescript.Text=“双因素身份验证或2FA为基本登录过程添加了一个额外步骤。” 情况是=4 结束选择 我提供的图片显示了当我选择应用程序中的第一个按钮时需要执行的操作

要说明的图像:

用户选择按钮后,照片将显示为按钮,并在右侧显示“关闭”以及名称和说明。

在您的代码中,如下所示

'Button2.Enabled = False

以及您的select case语句

Select Case randomChoice
    Case 1
    Case 2
    Case 3
    Case 4

当然包括每门课程的代码。

我认为您应该寻求一种更基于数据而不是代码的解决方案。您的代码似乎硬连接了大量类似或重复的逻辑,而您只需将所有可能的数据存储在数据结构中,并将其重新排列即可。然后,您将只使用该代码来显示经过洗牌的结果

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

照片是否未链接?是否有我丢失的任何信息对解决此问题更有用?代码“不正确”。如果没有看到您的代码,我不确定您希望我们能做什么。根据上面的代码,我试图为“btn2”分配一个随机值,然后使用该值选择要使用的案例。在您的声明中,Public Sub PresentTip(索引为整数)如何声明TipList(I)的“I”。Image@cfonte我认为TipList(I)应该是TipList(Index)。对不起,谢谢你的帮助,索引成功了。由此产生的一个小问题是,应用程序顶部的my menustrip显示为重复文件,Edit和Exit显示两次,并覆盖在“start”按钮的顶部。@cfonte我认为问题是因为InitializeComponent()被调用了两次。我认为在重写OnLoad时有必要调用它,但我错了。如果重写窗体的构造函数而不是OnLoad函数,则应显式调用InitializeComponent。只需删除
InitializeComponent()
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