我的目标是在单击我能够执行的按钮时生成随机问题(+,*或 - )和答案。问题是,当我生成它时,它返回一个问题(标签)和一个答案(MessageBox),但是当我在messageBox上单击确定时,它会直接询问另一个问题,一旦我解雇了它,它就会停止询问。
Private Sub Generate()
'This is my first random value'
Dim rnd1 As New Random
'This is my Second random Value'
Dim rnd2 As New Random
'This value will decide weather it is a +, * or -'
Dim rnd3 As New Random
'Declaring first value as an integer'
Dim Val1 As Integer
'Declaring second value as an integer'
Dim Val2 As Integer
'This will calculate the answer'
Dim Ans As Double
'This is what I will reference to to display the question'
Dim question As String
'If the random value is equal to 1, the question is an addition'
If (rnd3.Next(1, 3) = 1) Then
Val1 = rnd1.Next(1, 20)
Val2 = rnd2.Next(1, 25)
Ans = Val1 + Val2
question = Val1.ToString() + "+ " + Val2.ToString()
lbl_ques.Text = question
MessageBox.Show("Answer = " + Ans.ToString())
Val1 = 0
Val2 = 0
Ans = 0
question = ""
End If
'If the random value is equal to 2, the question is a multiplication'
If (rnd3.Next(1, 3) = 2) Then
Val1 = rnd1.Next(1, 10)
Val2 = rnd2.Next(1, 17)
Ans = Val1 * Val2
question = Val1.ToString() + "* " + Val2.ToString()
lbl_ques.Text = question
MessageBox.Show("Answer = " + Ans.ToString())
Val1 = 0
Val2 = 0
Ans = 0
question = ""
End If
'If the random value is equal to 3, the question is a subtraction'
If (rnd3.Next(1, 3) = 3) Then
Val1 = rnd1.Next(1, 50)
Val2 = rnd2.Next(1, 43)
Ans = Val1 - Val2
question = Val1.ToString() + "- " + Val2.ToString()
lbl_ques.Text = question
MessageBox.Show("Answer = " + Ans.ToString())
Val1 = 0
Val2 = 0
Ans = 0
question = ""
End If
End Sub
如何阻止它产生另一个问题并立即回答?感谢
如果您想知道我为什么使用VB,那是因为它用于学校项目并且必须使用VB:/我通常使用c#
答案 0 :(得分:1)
使用
退出当前子程序的退出子
If ... Then
Exit Sub
End If
或和ElseIf只执行一个If条件
If ... Then
ElseIf ... Then
End If
或(最好)选择案例陈述
Select Case rnd.Next(1, 3)
Case 1
Case 2
Case 3
End Select
旁注你只需要创建一个Random对象,每次只需要调用带有适当值的.Next