如何在VB.net中存储和计算数学测验的分数?

时间:2014-12-10 16:24:50

标签: vb.net

我对VB很陌生并正在做一个小型学校项目,我正在做一个数学测验,作为用户随机数学问题,如果他们得到正确的问题,那么他们的分数将增加1,我不知道如何计算得分。这是我到目前为止的代码:

Public Class Form1
    Dim Ans As Integer
    Dim Num As Integer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CreateProblem()
        CreateScore()
    End Sub

    Sub CreateProblem()
        Dim Generator As New Random
        Dim Num1 As Integer = Generator.Next(15, 20)
        Dim Num2 As Integer = Generator.Next(0, 15)
        Dim Show As String = ""
        Dim SumType As Integer = Generator.Next(1, 3)
        If SumType = 1 Then 
            Show = Num1 & "+" & Num2 & "= ?"
            Ans = Num1 + Num2
        ElseIf SumType = 2 Then 
            Show = Num1 & "-" & Num2 & "= ?"
            Ans = Num1 - Num2
        ElseIf SumType = 3 Then
            Show = Num1 & "*" & Num2 & "= ?"
            Ans = Num1 * Num2
        End If
        Label4.Text = Show


    End Sub
    Sub CreateScore()
        Dim Score As Integer
        If Ans = Val(txtAnswer.Text) Then 
            Score = Score + 1 
        End If
        If Score >= 10 Then 
            MsgBox("Well Done! You have completed the quiz!")
        End If
        lblScore.Text = Score 
    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        Call CreateProblem()
        txtAnswer.Text = ""
    End Sub

    Private Sub txtAnswer_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAnswer.TextChanged

    End Sub

    Private Sub Label4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label4.Click

    End Sub

    Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click
        If Val(txtAnswer.Text) = Ans Then 
            MsgBox("Correct")
        Else
            MsgBox("Incorrect") 
        End If
        Call CreateScore()
    End Sub
End Class

1 个答案:

答案 0 :(得分:1)

移动线

Dim Score As Integer

在全球范围内,你有Ans和Num变量吗? 这样,存储在此变量中的值可在调用CreateScore方法

之间使用

当然,现在你还有另外一个问题。当用户开始新测试时,您需要一种方法来重置此值。可能你应该添加一个清除该值的按钮并将其设置为零

此处有关Variable declaration in VB.NET

的相关文档