简体   繁体   中英

Visual Basic - Multiple Choice Test

I am really new to visual studio and I also want to learn to make a test like this but so far I managed to make 2 questions and 4 answers each. What I want to know is how do I check if the good answer is checked for more than 2 questions. how do I make button5 to change text to do what button1 does, so instead of having two buttons from the start, just one that changes.

My code so far is :

Public Class Test1
    Dim question(2, 5) As String
    Dim i As Integer = 2
    Private Sub Test1_Load()
        question(1, 0) = "2+2="
        question(1, 1) = "1"
        question(1, 2) = "2"
        question(1, 3) = "3"
        question(1, 4) = "4"
        question(2, 0) = "How old are you?"
        question(2, 1) = "12"
        question(2, 2) = "13"
        question(2, 3) = "17"
        question(2, 4) = "18"
        Label1.Text = question(i - 1, 0)
        nr1.Text = question(i - 1, 1)
        nr2.Text = question(i - 1, 2)
        nr3.Text = question(i - 1, 3)
        nr4.Text = question(i - 1, 4)

    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        Test1_Load()
        Button5.Hide()

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If i = 2 AndAlso nr4.Checked = True Then
            MessageBox.Show("Good job. You have one point")
        ElseIf i = 2 AndAlso nr4.Checked = False Then
            MessageBox.Show("Sorry. You are wrong")
        ElseIf i = 3 AndAlso nr4.Checked Then
            MessageBox.Show("Good job. Another point")
        End If
        i = i + 1
        Test1_Load()

    End Sub
End Class

You can use a variable to store if the first answer was correct like

Dim correctAnswers = 0

and then in your button click method,

If i = 2 AndAlso nr4.Checked = True Then
    correctAnswers+= 1 'increment correctAnswers by 1
    MessageBox.Show("Good job. You have one point")
ElseIf i = 2 AndAlso nr4.Checked = False Then
    MessageBox.Show("Sorry. You are wrong")
ElseIf i = 3 AndAlso nr4.Checked Then
    correctAnswers += 1 'increment correctAnswers by 1
    MessageBox.Show("Good job. Another point")
End If
i = i + 1
Test1_Load()

You could also add something like

If i > 3 Then 'if i > 3, then you have run out of questions
    MessageBox.Show("That's the end of the test. You answered " + correctAnswers.ToString() + " questions correctly.")
Else 'if i < 3, then there are still questions to answer
    'Put the rest of your code here
End If

in your button click method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM