Visual Basic - 多项选择测试

时间:2017-05-11 16:01:30

标签: vb.net

我是视觉工作室的新手,我也想学习这样的测试,但到目前为止,我设法分别提出2个问题和4个答案。我想知道的是 如何检查是否检查了2个以上的问题。 如何让button5更改文本以执行button1所做的操作,因此不是从一开始就有两个按钮,而只是一个更改。

到目前为止我的代码是:

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

1 个答案:

答案 0 :(得分:2)

如果第一个答案是正确的,可以使用变量来存储,如

Dim correctAnswers = 0

然后在按钮单击方法中

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()

您还可以添加类似

的内容
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

按钮点击方法。