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

如何在VB.net中存储和计算数学测验的分数?,vb.net,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

我对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
把线移开

Dim Score As Integer
在全局级别,您对Ans和Num变量具有相同的属性。
这样,存储在该变量中的值在调用CreateScore方法之间可用

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


这里是关于

的相关文档好的,谢谢,伙计,明天我可能会遇到另一个问题:D