如果userAnswer = correctAnswer如何循环 [英] How to loop if userAnswer = correctAnswer

查看:94
本文介绍了如果userAnswer = correctAnswer如何循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在使用Try ... Catch代码时遇到了麻烦.我试图使文本框仅接受字母A,B,C或D作为用户的答案.我也很难匹配两个数组.我希望它像如果textbox1是A,那么它是正确的,但是处于循环状态.请看一下我的代码.任何帮助表示赞赏.谢谢.

这是编程挑战性说明,仅供参考:
当地的汽车登记处要求您创建一个应用程序,以对驾驶执照考试的书面部分进行评分.该考试有20个多项选择题.以下是对问题的正确答案:
1. B 6. A 11. B 16. C
2. D 7. B 12. C 17. C
3. A 8. A 13. D 18. B
4. A 9. C 14. A 19. D
5. C 10.D 15. D 20. A

Hi,

I''m having trouble with the Try...Catch code. I''m trying to make the textboxes only accept letters A, B, C, or D as answers from the user. I''m also having trouble matching the two arrays. I wanted it to be like if textbox1 is A then it is correct but in a loop. Please take a look at my code. Any help is appreciated. Thank you.

This is the programming challenge instruction for reference:
The local Registry of Motor Vehicles office has asked you to create an application that grades the written portion of the driver''s license exam. The exam has 20 multiple choice questions. Here are the correct answers to the questions:
1. B 6. A 11. B 16. C
2. D 7. B 12. C 17. C
3. A 8. A 13. D 18. B
4. A 9. C 14. A 19. D
5. C 10.D 15. D 20. A

Public Class frmJPDLExamm
    Dim intMinutes As Integer   'Holds the minutes for the timer
    Private Sub btnScore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnScore.Click
        Dim AnswersForm As New frmAnswers
        'Correct answers as an array
        Dim achrCorrectAnswers() As Char = {"B", "D", "A", "A", "C", "A", "B", "A", "C", "D", "B", "C", "D", "A", "D", "C", "C", "B", "D", "A"}
        Dim intIncorrect As Integer     'Counter for incorrect answers
        Dim intCorrect As Integer       'Counter for correct answers
        Dim intCount As Integer         'Total counter
        Dim chrInput As Char            'Hold a char value
        Dim achrUserAnswers(19) As Char 'Textboxes as an array
        achrUserAnswers(0) = txt1.Text
        achrUserAnswers(1) = txt2.Text
        achrUserAnswers(2) = txt3.Text
        achrUserAnswers(3) = txt4.Text
        achrUserAnswers(4) = txt5.Text
        achrUserAnswers(5) = txt6.Text
        achrUserAnswers(6) = txt7.Text
        achrUserAnswers(7) = txt8.Text
        achrUserAnswers(8) = txt9.Text
        achrUserAnswers(9) = txt10.Text
        achrUserAnswers(10) = txt11.Text
        achrUserAnswers(11) = txt12.Text
        achrUserAnswers(12) = txt13.Text
        achrUserAnswers(13) = txt14.Text
        achrUserAnswers(14) = txt15.Text
        achrUserAnswers(15) = txt16.Text
        achrUserAnswers(16) = txt17.Text
        achrUserAnswers(17) = txt18.Text
        achrUserAnswers(18) = txt19.Text
        achrUserAnswers(19) = txt20.Text

        Try
            'Convert the user input from a textbox to a char
            strInput = CChar(achrUserAnswers(19))
            For intCount = 0 To 19
                'If the user's answer from the textbox is equals to the correct answer
                'from the corresponding array, then the answer is correct
                If achrUserAnswers(19) = achrCorrectAnswers(19) Then
                    intCorrect += 1
                    AnswersForm.lstAns.Items.Add("Correct")
                    'Else, the user's answer is incorrect
                Else
                    intIncorrect += 1
                    AnswersForm.lstAns.Items.Add("Incorrect")
                    intCount += 1
                End If
            Next
        Catch
            'Error message for invalid input
            MessageBox.Show("Enter letter A, B, C, or D only.")
        End Try

        'Display the total incorrect answers in the listbox
        AnswersForm.lstAns.Items.Add("You have a total of " & intIncorrect.ToString() & " Incorrect answers.")
        intCorrect = 20 - intIncorrect
        'Display the total correct answers in the listbox
        AnswersForm.lstAns.Items.Add("and a total of " & intCorrect.ToString() & " Correct answers.")

        'If the user has 15 or more correct answers, then the user pass the exam
        If intCorrect > 14 Then
            AnswersForm.lstAns.Items.Add("Congratulations! You have passed the Driver's License Exam.")
            'Else, the user fail the exam
        Else
            AnswersForm.lstAns.Items.Add("You have failed the Driver's License Exam. Try Again.")
        End If

        'Display the results in another form
        AnswersForm.ShowDialog()
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        'Clears all textboxes. From textbox 1 to 20.
        txt1.Clear()
        txt2.Clear()
        txt3.Clear()
        txt4.Clear()
        txt5.Clear()
        txt6.Clear()
        txt7.Clear()
        txt8.Clear()
        txt9.Clear()
        txt10.Clear()
        txt11.Clear()
        txt12.Clear()
        txt13.Clear()
        txt14.Clear()
        txt15.Clear()
        txt16.Clear()
        txt17.Clear()
        txt18.Clear()
        txt19.Clear()
        txt20.Clear()
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        'Close the form
        Me.Close()
    End Sub
    Private Sub btnToggle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnToggle.Click
        'Toggle the timer
        If tmrMinutes.Enabled = True Then
            tmrMinutes.Enabled = False
            btnToggle.Text = "Start &Timer"
        Else
            tmrMinutes.Enabled = True
            btnToggle.Text = "Stop &Timer"
        End If
    End Sub

    Private Sub tmrMinutes_Tick_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrMinutes.Tick
        'Update the minutes display by one minute every minute
        intMinutes += 1
        lblCounter.Text = intMinutes.ToString()
    End Sub
End Class

推荐答案

在多数情况下答案是多项选择的情况下,您可以使用 ^ ]或 RadioButton [ ^ ]控制...
您要完成的工作非常不寻常且不是很直观,但是如果您确实想执行此操作,请检查以下内容:转换器 [
In most cases where the answer is multiple choice you would use either a CheckBox[^] or a RadioButton[^] Control...
What you are trying to accomplish is pretty unusual and not very intuitive, but if you really want to do it, check this: Text box to accept only number[^]
Those TextBoxes accept only numbers, but of course you could also check for A, B, C or D instead of for number. The code is C#, but a convertor[^] should help you translate it to VB. It''s just a little bit of code.
Hope it helps.


这篇关于如果userAnswer = correctAnswer如何循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆