提问人:Connorjones91 提问时间:11/25/2014 最后编辑:Ňɏssa PøngjǣrdenlarpConnorjones91 更新时间:11/25/2014 访问量:963
用于随机数生成器的 Visual Basic 文本框数组 - 用于家庭作业
Visual Basic Text Box Array for random number generator - For Homework
问:
我想做一个随机数生成器。这是为了学校的家庭作业。我似乎无法让它工作。
任务是制作一个随机数生成器,将 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)
答:
0赞
Idle_Mind
11/25/2014
#1
“高效”是相当主观的。有很多方法可以做到这一点:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
RandomizeTextBoxes(1, 12)
End Sub
Private Sub RandomizeTextBoxes(ByVal Low As Integer, ByVal High As Integer)
Static R As New Random
For i As Integer = 1 To 30
Dim matches() As Control = Me.Controls.Find("Rnd" & i, True)
If matches.Length > 0 AndAlso TypeOf matches(0) Is TextBox Then
DirectCast(matches(0), TextBox).Text = R.Next(Low, High + 1)
End If
Next
End Sub
0赞
Brett Caswell
11/25/2014
#2
与 @Idle_Mind 的答案一样,它使用 Textbox 的 Name 属性来确定是否应为 Text 属性分配生成的值。
这里的区别在于我通过控件集合进行枚举;在 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
评论
0赞
Brett Caswell
11/25/2014
通过将 addressof 函数传递给集合(通过 where 方法),我传递了一个预测函数,该函数将针对集合中的每个对象进行测试,以及由此产生的在该谓词中返回 true 的对象的新集合。这种编程风格称为逆变编程。DoesTextboxNameContainRnd
评论