2014-12-02 35 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