2014-12-02 72 views
0

我的目标是生成一个随机问题(+,*或 - )和一个答案,当我点击一个我已经能够做到的按钮时。问题是,当我生成它时,它会返回一个问题(标签)和一个答案(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 

我怎样才能阻止它直接产生另一个问题和答案?由于

PS如果你想知道为什么我用VB这是因为它的一个学校项目,必须是在VB:/我通常使用C#

+0

当它会工作,将其发送到http://codereview.stackexchange.com/你会得到一些关于如何抛光你的代码的好建议。 – 2014-12-02 16:11:24

回答

1

要么使用

的出口子打破出当前子程序

If ... Then 

    Exit Sub 
End If 

或和ELSEIF的仅执行的。如果条件之一

If ... Then 

ElseIf ... Then 

End If 

或(最好)选择一个case语句

Select Case rnd.Next(1, 3) 

Case 1 

Case 2 

Case 3 

End Select 

侧面说明,你只需要创建一个随机对象并调用。接下来用适当的值每次

+0

谢谢,作品太棒了! :) – Legitimat3 2014-12-02 15:56:38