Reputation: 13
I'm quite new to VB and am doing a small school project, I am making a math quiz that will as the user random math questions and if they get the question correct then their score will increase by 1, i do not know how to count the score though. This is the code i have so far:
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
Upvotes: 1
Views: 1785
Reputation: 216273
Just move the line
Dim Score As Integer
at the global level has you have for the Ans and Num variables.
In this way the value stored in this variable is available between calls to your CreateScore method
Of course, now you have another problem. You need a way to RESET this value when your user starts a new test. Probably you should add a button the clear that value and sets it to zero
Here the relevant documentation about Variable declaration in VB.NET
Upvotes: 1