将分数添加到一组测验按钮

时间:2018-10-25 02:04:10

标签: vb.net

我正在尝试为我的多项选择测验建立一个评分系统,但是有时当我单击正确的答案时分数不会增加,或者当我单击错误的答案时分数会增加。

表1排列了问题和答案,并创建了一个随机数发生器

Public Class Form1
    Public StudentName As String
    Public Formclass As String
    Public questions(19) As String
    Public answers(19) As String
    Public x As Integer
    Public rn As New Random


    Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Questions
        questions(0) = "What is the biggest win in soccer"
        questions(1) = "What colour is Real Madrid's 2017-2018 season away kit"
        questions(2) = "Neymar Plays for PSG" 'T/F
        questions(3) = "What year did Manchester United sign Robin Van Persie"

        'Answers
        answers(0) = "31-0"
        answers(1) = "Black"
        answers(2) = "True"
        answers(3) = "2012"

    End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnStart.Click
        StudentName = txtName.Text
        Formclass = txtForm.Text
        x = (rn.Next(0, 19))
        Form2.RichTextBox1.Text = questions(x)
        Me.Hide()
        Form2.Show()
    End Sub

表格2是我的大部分代码,我在其中设置问题以及应在何处计算分数。我这样做的方式是,如果文本框=我以一个特定的答案集形式设置的问题应显示,并且如果显示了某个问题,则应在每个按钮上显示特定的答案集,其中一个按钮为答案。

Public Class Form2
    Public Score As Integer
    Public Answered As Integer
    Public Correct As Integer
    Public Percent As Integer
    Public Grade As String
    Public Message As String

    Sub fillanswers()


        If RichTextBox1.Text = Form1.questions(0) Then
            Button1.Text = Form1.answers(0)
            Button2.Text = "41-3"
            Button3.Text = "7-0"
            Button4.Text = "27-2"



        ElseIf RichTextBox1.Text = Form1.questions(1) Then
            Button1.Text = "Pink"
            Button2.Text = "Grey"
            Button3.Text = Form1.answers(1)
            Button4.Text = "White"

        ElseIf RichTextBox1.Text = Form1.questions(2) Then
            Button1.Text = Form1.answers(2)
            Button2.Text = "False"
            Button3.Text = ""
            Button4.Text = ""

        ElseIf RichTextBox1.Text = Form1.questions(3) Then
            Button1.Text = "2009"
            Button2.Text = "2010"
            Button3.Text = "2011"
            Button4.Text = Form1.answers(3)     

        End If
    End Sub
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        fillanswers()
        Score = 0
        Answered = 0
    End Sub


    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        ChangeQuestion()
        fillanswers()
        If RichTextBox1.Text = Form1.questions(12) Or RichTextBox1.Text = Form1.questions(8) Or RichTextBox1.Text = Form1.questions(3) Then
            Score = Score + 1
            Label1score.Text = Score
        End If


    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ChangeQuestion()
        fillanswers()
        If RichTextBox1.Text = Form1.questions(0) Or RichTextBox1.Text = Form1.questions(2) Or RichTextBox1.Text = Form1.questions(6) Or RichTextBox1.Text = Form1.questions(7) Or RichTextBox1.Text = Form1.questions(9) Or RichTextBox1.Text = Form1.questions(13) Or RichTextBox1.Text = Form1.questions(14) Or RichTextBox1.Text = Form1.questions(18) Or RichTextBox1.Text = Form1.questions(19) Then
            Score = Score + 1
            Label1score.Text = Score
        End If


    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        ChangeQuestion()
        fillanswers()
        If RichTextBox1.Text = Form1.questions(4) Or RichTextBox1.Text = Form1.questions(2) Or RichTextBox1.Text = Form1.questions(15) Then
            Score = Score + 1
            Label1score.Text = Score
        End If


    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        ChangeQuestion()
        fillanswers()
        If RichTextBox1.Text = Form1.questions(1) Or RichTextBox1.Text = Form1.questions(5) Or RichTextBox1.Text = Form1.questions(10) Or RichTextBox1.Text = Form1.questions(16) Then
            Score = Score + 1
            Label1score.Text = Score
        End If



    End Sub

    Sub ChangeQuestion()
        Form1.x = (Form1.rn.Next(0, 19))
        RichTextBox1.Text = Form1.questions(Form1.x)
    End Sub

1 个答案:

答案 0 :(得分:0)

参考我的最后一条评论,如果您可以重命名控件以使用jmcilhinney建议的有意义的名称,这将非常有帮助。这将使我们更容易理解您的代码。

您遇到问题的可能性有很多,其中之一就是这种情况。

我猜想,当您单击button2时,您想根据问题来检查用户输入的文本。问题是您的代码顺序可能不正确。更改问题和答案之前,您应该先检查答案。

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    If RichTextBox1.Text = Form1.questions(4) Or RichTextBox1.Text = Form1.questions(2) Or RichTextBox1.Text = Form1.questions(15) Then
        Score = Score + 1
        Label1score.Text = Score
    End If
    ChangeQuestion()
    fillanswers()
End Sub

但是,我建议以面向对象的方式考虑事物。例如,您的代码将每个问题和每个正确答案存储在单独的对象(两个数组)中,更好的方法是创建一个具有问题,正确答案和最多3个错误答案属性的问题类。这样,相关的信息便被链接在一起,并且更易于处理..