Monday, July 27, 2009

Kindly help me to improve my codes..that if they will input wrong ans. the problem will remain the same..?

Option Explicit


Dim a As Integer


Dim b As Integer


Dim c As Integer


Dim d As Integer


Const vbKeyDecPt = 46








Private Sub cmdexit_Click()


End


End Sub





Private Sub cmdnext_Click()


c = Val(txtans.Text)


If c = a + b Then


lblmessage.Caption = "Correct Answer"


Text2.Text = d + 1


ElseIf txtans.Text = "" Then


lblmessage.Caption = "Pls. Input Answer"


Else


lblmessage.Caption = "Wrong Answer!"


End If








d = Text2.Text


a = (50 * Rnd)


b = (49 * Rnd)





lblnum1.Caption = a


lblnum2.Caption = b


txtans.Text = ""





End Sub





Private Sub Command1_Click()


Text2.Text = ""


End Sub





Private Sub Command2_Click()





End Sub





Private Sub Form_Load()


cmdnext.Enabled = True


a = (50 * Rnd)


b = (49 * Rnd)





lblnum1.Caption = a


lblnum2.Caption = b





End Sub








Private Sub txtans_KeyPress(KeyAscii As Integer)


If (KeyAscii %26gt;= vbKey0 And KeyAscii %26lt;= vbKey9) Or KeyAscii = vbKeyDecPt Or KeyAscii = vbKeyBack Then


Exit Sub


Else


KeyAscii = 0


Beep


End If





End

Kindly help me to improve my codes..that if they will input wrong ans. the problem will remain the same..?
Why do not you contact a VB expert? Check http://k.aplis.net/
Reply:Simple enough. I've even got two methods for you. First method:


Initialize a boolean flag variable at the top of btnNext.click's procedure to false. Whenever you test if the user entered the wrong answer, modify the flag accordingly (true if answer was right, false if wrong). Down where you change the numbers (ie a = (50*rnd) b = (49*rnd)), put an if...then block around it, where at you test the flag for truth or falseness (if booFlag = true then a=(50*rnd) b = (49*rnd)).





The second method, and one I recommend (saves coding) is to simply move the re-definitions of a and b (a=(50*rnd) and all that stuff) into the if..then block (after the pre-existing code for a correct answer) where you test if the user inputted the correct answer. Thus, whenever you test for the correct answer, you will automatically generate a new question if the answer is correct (elsewise it will "fall through the bottom" and the message you sent to lblMessage will tell the user of the problem).


No comments:

Post a Comment