Problems with If statement or Loop?

  • Thread starter Thread starter alkmenel
  • Start date Start date
A

alkmenel

Guest
This is my first time using Visual basic hence im a newbie programmer. Im trying to make this Math game and I have a problem trying to get this variable(SCORE) to repeat itself in the "If Statement" Example: The first time i get the answer right the score appears but the second time right the score doesnt add up (+10).. (the bolded texts and underline is what i am having problems on) can anyone help me figure this out? suggestions? thanks. here is the code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click
Checks Answer
Answer = (rannum1 + rannum2)
If txtGuess.Text = "" Then
MsgBox("Enter the answer of the problem")
txtGuess.Focus()
Exit Sub
End If

If txtGuess.Text = Answer Then
Do
lblScore.Text = CInt(Score + 10)
Randomize()
rannum1 = Int(Rnd() * 50) + 1
Randomize()
rannum2 = Int(Rnd() * 50) + 1
txtNum1.Text = CInt(rannum1)
TxtNum2.Text = CInt(rannum2)
txtGuess.Text = ""
txtAnswer.Text = "???"
Exit Do
Loop

MsgBox("CORRECT!")
Else --etc...

Thanks
 
take out the DO .. LOOP, i dont see why you need that there.. iif the answer is correct, those operations will be performed, then the routine is done and awaiting the next entered answer....
 
youll have to change anything that needs to be changed for .NET

Code:
Option Explicit
Dim Score As Integer
Dim Rannum1 As Integer
Dim Rannum2 As Integer
Dim Answer As Integer

Private Sub GenerateNumbers()
Randomize
Rannum1 = Int(Rnd() * 50) + 1
Rannum2 = Int(Rnd() * 50) + 1


lblProblem is the math problem to be completed
use text boxes or whatever you want to display it
lblProblem.Caption = Rannum1 & "+" & Rannum2

Answer = (Rannum1 + Rannum2)

End Sub

Private Sub Command1_Click()

If txtGuess.Text = "" Then
    MsgBox ("Enter the answer of the problem")
    txtGuess.SetFocus
    Exit Sub
End If

If txtGuess.Text = Answer Then
    Score = Score + 10
    lblScore.Caption = Score
    MsgBox ("CORRECT!")
    txtGuess.Text = ""
    Call function to create a new problem:
    GenerateNumbers
Else
    MsgBox "Wrong answer, try again.", vbExclamation, "Wrong Answer"
    txtGuess.Text = ""
End If


End Sub

Private Sub Form_Load()
Initilize Score to 0
Score = 0
End Sub
 
Back
Top