【问题标题】:Visual Basic Text Box Array for random number generator用于随机数生成器的 Visual Basic 文本框数组
【发布时间】:2014-11-18 22:08:53
【问题描述】:

我想做一个随机数生成器。是为了学校作业。我似乎无法让它工作。

任务是制作一个随机数生成器,将 20 个数字生成到 20 个文本框中,以提出 10 个数学问题。

它需要尽可能高效。我需要帮助,因为我只能这样做,但它确实效率低下且代码很长。

Dim RandomNumberAHigh As Integer = 12 'Here I am declaring my highest number for the random number generator to use. This will be the highest number the maths quiz will ask the students in the questions.
Dim RandomNumberALow As Integer = 1 'Here I am declaring my lowest number for the random number generator to use. This will be the lowest number the maths quiz will ask the students in the questions.
Dim Random As Integer = 0 'Her I am declaring a variable that will be used to insert the numbers from the random number generator in the quiz text boxes.
Randomize()
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd1.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd4.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd7.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd10.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd13.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd16.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd19.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd22.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd25.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd28.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd3.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd6.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd9.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd12.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd15.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd18.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd21.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd24.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd27.Text = (Random)
Random = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
Rnd30.Text = (Random)

【问题讨论】:

    标签: vb.net random


    【解决方案1】:

    尝试对所有文本框使用 foreach 循环,如下所示:

    Dim RandomNumberAHigh As Integer = 12
    Dim RandomNumberALow As Integer = 1
    Dim Random As Integer = 0
    For Each txt As Control In Me.Controls
        If TypeOf txt Is TextBox Then
            txt.Text = Int((RandomNumberAHigh - RandomNumberALow + 1) * Rnd() + RandomNumberALow)
        End If
    Next
    

    【讨论】:

    • 不先使用Rnd() 不使用Randomize()
    【解决方案2】:

    我可能会遍历页面上的所有控件,检查它是否是文本框,如果是,则为其分配一个数字。此外,使用一个函数来生成您的号码以清理和加速它。

    对于您的速度问题,很可能是因为您每次都在创建一个新的随机实例。看看下面的函数。

      'Create a control object, loop through every control. If its a textbox, generate your number         
      Dim cControl As Control
        For Each cControl InMe.Controls
            If (TypeOf cControl Is textbox) Then
                cControl.Text = GetRandom(RandomNumberALow,RandomNumberAHigh)
            End If
        Next cControl
    
     Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer
     ' by making Generator static, we preserve the same instance '
     ' (i.e., do not create new instances with the same seed over and over) '
     ' between calls '
         Static Generator As System.Random = New System.Random()
         Return Generator.Next(Min, Max)
     End Function
    

    【讨论】:

      【解决方案3】:

      就像@Idle_Mind 的回答(在您的重复线程中......这可能会被删除),它使用文本框的名称属性来确定是否应该为文本属性分配生成的值。

      这里的区别是我是通过控件集合来枚举的;在 For Each 循环中使用 IEnumerable。通过这种方式,我不必检查控件集合、对照控件集合的长度,或者在循环处理中强制转换控件。

      Sub RandomizeTextBoxes(min As Integer, max As Integer)
          Dim r As Random = New Random()
          Dim RandomTextboxes As IEnumerable(Of TextBox) = Me.Controls.OfType(Of TextBox).Where(AddressOf DoesTextboxNameContainRnd)
          For Each tbControl As TextBox In RandomTextboxes
              tbControl.Text = r.Next(min, max)
          Next
      End Sub
      
      Function DoesTextboxNameContainRnd(tbControl As TextBox) As Boolean
          Return tbControl.Name.Contains("Rnd")
      End Function
      
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
          RandomizeTextBoxes(1, 50)
      End Sub
      

      通过将DoesTextboxNameContainRnd 函数的地址传递给集合(通过 where 方法),我传递了一个预测函数,该函数将针对集合中的每个对象进行测试,并生成新的对象集合在该谓词中返回 true。这种编程风格称为逆变编程。

      【讨论】:

      • 此外,可以使用这种样式来修改文本框集合的 textbox.text 值,因此根本不需要执行 for 循环。
      猜你喜欢
      • 1970-01-01
      • 2014-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多