使用Random
类:(编辑以显示更简洁的版本)
Dim grade As Double
If Double.TryParse(txtGrade.Text, grade) Then
Dim values = {(grade * 40 / 100).ToString(), (grade * 20 / 100).ToString(), (grade * 30 / 100).ToString(), (grade * 10 / 100).ToString()}
FillRandomControlText({txtA, txtB, txtC, txtD}, values)
End If
用这个方法:
Shared Sub FillRandomControlText(txtControls As IList(Of TextBox), values As IList(Of String))
Dim rnd = New Random()
Dim txt As TextBox
Dim selectedControls = New List(Of Control)(txtControls.Count)
For i As Int32 = 0 To txtControls.Count - 1
Do
txt = txtControls(rnd.Next(txtControls.Count))
Loop While selectedControls.Contains(txt)
selectedControls.Add(txt)
txt.Text = values(i)
Next
End Sub
Dim Grade As Double Dim rnd = New Random() Dim controls = {txtA, txtB, txtC, txtD} Dim selectedControls = New List(Of Control) Dim txt As TextBox
If Double.TryParse(txtGrade.Text, grade) Then
Do
txt = controls(rnd.Next(controls.Length))
Loop While selectedControls.Contains(txt)
selectedControls.Add(txt)
txt.Text = (grade * 40 / 100).ToString()
Do
txt = controls(rnd.Next(controls.Length))
Loop While selectedControls.Contains(txt)
selectedControls.Add(txt)
txt.Text = (grade * 20 / 100).ToString()
Do
txt = controls(rnd.Next(controls.Length))
Loop While selectedControls.Contains(txt)
selectedControls.Add(txt)
txt.Text = (grade * 30 / 100).ToString()
Do
txt = controls(rnd.Next(controls.Length))
Loop While selectedControls.Contains(txt)
txt.Text = (grade * 10 / 100).ToString()
End If