2016-12-10 63 views
1

所以我是一個極端的初學者在Visual Basic和我試圖做一個簡單的4問題測驗與單選按鈕和一個標籤,允許用戶選擇一個問題的答案被提出,然後記錄他們有多少問題正確回答。我決定使用循環來計算用戶在哪個問題上,以及他們有多少人回答正確。我必須在這裏丟失一些明顯的東西,因爲當我點擊按鈕來啓動這段代碼時,程序完全凍結。當我使用這些循環時,這個程序爲什麼會凍結?

我在做什麼錯? (道歉,如果這太模糊的問題)

Private Sub QuizButton1_Click(sender As Object, e As EventArgs) Handles QuizButton1.Click 
     Dim question As Integer = 0 
     Dim correct As Integer = 0 
     Do Until question = 4 

      While question = 0 
       QuizLabel1.Text = "How much force do the Great Horned Owl's talons put out while clenched? A. 28 pounds B. 13 pounds C. 200 pounds D. 20 pounds" 
       If Abutton1.Checked Then 
        question = 1 
        correct = correct + 1 
       ElseIf Bbutton1.Checked Then 
        question = 1 
       ElseIf Cbutton1.Checked Then 
        question = 1 
       ElseIf Dbutton1.Checked Then 
        question = 1 
       End If 
      End While 
      While question = 1 
       QuizLabel1.Text = "What's a nickname for the Great Horned Owl? A. Lion Owl B. Tiger Owl C. Hawk Owl D. Cat Owl " 
       If Abutton1.Checked Then 
        question = 2 
       ElseIf Bbutton1.Checked Then 
        question = 2 
        correct = correct + 1 
       ElseIf Cbutton1.Checked Then 
        question = 2 
       ElseIf Dbutton1.Checked Then 
        question = 2 
       End If 
      End While 
      While question = 2 
       QuizLabel1.Text = "Why is this owl called 'Horned'? A. It has small horns B. It has pointy ears C. Common folklore D. It has feathery tufts on its head" 
       If Abutton1.Checked Then 
        question = 3 
       ElseIf Bbutton1.Checked Then 
        question = 3 

       ElseIf Cbutton1.Checked Then 
        question = 3 
       ElseIf Dbutton1.Checked Then 
        question = 3 
        correct = correct + 1 
       End If 

      End While 
      While question = 3 
       QuizLabel1.Text = "What's the maximum recorded length of a Great Horned Owl? A. 20.4 inches B. 15.8 inches C. 12.3 inches D. 24.8 inches" 
       If Abutton1.Checked Then 
        question = 4 
       ElseIf Bbutton1.Checked Then 
        question = 4 

       ElseIf Cbutton1.Checked Then 
        question = 4 
       ElseIf Dbutton1.Checked Then 
        question = 4 
        correct = correct + 1 
       End If 
      End While 
     Loop 
     Dim score As Integer 
     score = correct * 25 
     QuizLabel1.Text = "Thanks for taking the quiz! You scored a " & score & "%. Press the button below to play again." 
    End Sub 
+0

你的代碼放入其中並沒有結束,這就是爲什麼它凍結 – Aravind

+0

你需要退出循環的循環* QuizButton1_Click *其他業務進行表單上如單擊按鈕和這樣。 –

+0

我能做什麼特別的代碼來阻止它無限循環?我將如何退出QuizButton1_Click? – davidib17

回答

2

我不是VB專家,但我可能能夠在這裏提供一些幫助。如果我不得不猜測,我會說你的程序正在鎖定,因爲它處於一個無限循環中。我認爲該循環正在不斷評估,並且不允許其他任何事情。我會使用事件處理程序,並在用戶單擊答案時觸發評估。它看起來像你有一個事件處理程序來啓動測驗,但接着循環接管。我不確定你的佈局究竟是什麼,但我使用的按鈕爲你的a,b,c和d awnsers。當你點擊按鈕時,它會觸發一個事件並進行相應的處理。我試圖讓這個容易理解。有更多優雅的方式來實現這一點,但這會起作用。嘗試財產以後這樣的:

Public Class Form1 

    Dim question As Integer 
    Dim correct As Integer 

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     correct = 0 
     Label2.Text = correct 

     Abutton1.Hide() 
     Bbutton1.Hide() 
     Cbutton1.Hide() 
     Dbutton1.Hide() 
    End Sub 

    Private Sub QuizButton1_Click(sender As Object, e As EventArgs) Handles QuizButton1.Click 
     question = 0 
     QuizLabel1.Text = "How much force do the Great Horned Owl's talons put out while clenched? A. 28 pounds B. 13 pounds C. 200 pounds D. 20 pounds" 
     QuizButton1.Hide() 
     Abutton1.Show() 
     Bbutton1.Show() 
     Cbutton1.Show() 
     Dbutton1.Show() 
    End Sub 

    Private Sub Abutton1_Click(sender As Object, e As EventArgs) Handles Abutton1.Click 
     test(1) 
    End Sub 

    Private Sub Bbutton1_Click(sender As Object, e As EventArgs) Handles Bbutton1.Click 
     test(2) 
    End Sub 

    Private Sub Cbutton1_Click(sender As Object, e As EventArgs) Handles Cbutton1.Click 
     test(3) 
    End Sub 

    Private Sub Dbutton1_Click(sender As Object, e As EventArgs) Handles Dbutton1.Click 
     test(4) 
    End Sub 

    Private Sub test(button) 

     Select Case question 
      Case 0 
       If button = 1 Then 
        correct = correct + 1 
        Label2.Text = correct 
       End If 

       question = question + 1 
       QuizLabel1.Text = "What's a nickname for the Great Horned Owl? A. Lion Owl B. Tiger Owl C. Hawk Owl D. Cat Owl " 
      Case 1 
       If button = 1 Then 
        correct = correct + 1 
        Label2.Text = correct 
       End If 

       question = question + 1 
       QuizLabel1.Text = "What's a nickname for the Great Horned Owl? A. Lion Owl B. Tiger Owl C. Hawk Owl D. Cat Owl " 
      Case 2 
       If button = 4 Then 
        correct = correct + 1 
        Label2.Text = correct 
       End If 

       question = question + 1 
       QuizLabel1.Text = "Why is this owl called 'Horned'? A. It has small horns B. It has pointy ears C. Common folklore D. It has feathery tufts on its head" 
     End Select 

    End Sub 

End Class 
相關問題