我正在尝试编写一个具有级别(或阶段)的数学计算器,我有5个复选框,一次只能选中一个。我找不到一种编码方式,可以检查复选框是否已选中,然后执行随机数生成器。请帮我。这是我的代码:
If Stage1.Checked = True Then
Randomize()
Random1 = Int(Rnd() * 5) + 1
Randomize()
Random2 = Int(Rnd() * 5) + 1
Random1 = q1.Text
Random2 = q2.Text
ElseIf Stage2.Checked = True Then
Randomize()
Random1 = Int(Rnd() * 6) + 1
Randomize()
Random2 = Int(Rnd() * 6) + 1
Random1 = q1.Text
Random2 = q2.Text
ElseIf Stage3.Checked = True Then
Randomize()
Random1 = Int(Rnd() * 9) + 1
Randomize()
Random2 = Int(Rnd() * 9) + 1
Random1 = q1.Text
Random2 = q2.Text
ElseIf Stage4.Checked = True Then
Randomize()
Random1 = Int(Rnd() * 10) + 1
Randomize()
Random2 = Int(Rnd() * 10) + 1
Random1 = q1.Text
Random2 = q2.Text
ElseIf Stage5.Checked = True Then
Randomize()
Random1 = Int(Rnd() * 12) + 1
Randomize()
Random2 = Int(Rnd() * 12) + 1
Random1 = q1.Text
Random2 = q2.Text
Else
MessageBox.Show("Please Select a Stage.")
End If
答案 0 :(得分:1)
Static rand As new Random()
Dim Factor As Integer = 0
If Stage1.Checked Then
Factor = 5
ElseIf Stage2.Checked Then
Factor = 6
ElseIf Stage3.Checked Then
Factor = 9
ElseIf Stage4.Checked Then
Factor = 10
ElseIf Stage5.Checked Then
Factor = 12
Else
MessageBox.Show("Please Select a Stage.")
Return
End If
q1.Text = rand.Next(1, Factor)
q2.Text = rand.Next(1, Factor)
很少使用的Static
关键字的引用:
https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/modifiers/static
我经常发现它是声明我的PRNG的有用方法。