我对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
发布于 2014-12-10 16:28:49
把线挪开
Dim Score As Integer
在全球范围内,您有Ans和Num变量。
以这种方式,存储在此变量中的值在调用CreateScore方法之间可用。
当然,现在你又遇到了另一个问题。当用户启动新测试时,您需要一种重置此值的方法。可能您应该添加一个按钮清除该值,并将其设置为零。
这里是有关VB.NET中的变量声明的相关文档
https://stackoverflow.com/questions/27406004
复制相似问题