2016-12-10 66 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 
相关问题