2016-12-10 9 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用のボタンを使用しています。ボタンをクリックすると、イベントが発生し、それに応じて処理されます。私はこれを分かりやすくしようとしました。これをより洗練させる方法がありますが、これはうまくいくでしょう。このように気にいらをお試しください:

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 
関連する問題