|
-
May 27th, 2011, 06:14 PM
#1
Thread Starter
Member
Correct / Incorrect
Been working through a maths game and have got so far but face my problem with the answers, no matter what input i put in ( numbers ofc ) it says there correct, i cant work out why its not doing what i coded it to do, if you can notice whats wrong your a star. 
Math game Code:
Public Class Form3
Dim RandomClass As New Random
Dim symbole As Integer
Dim Answers As Integer
Public Wrong_Answer As Integer
Public Total_Correct As Integer
Public Lifes As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
check()
End Sub
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lblname.Text = username
randomize()
End Sub
Sub randomize()
Int(Rnd() * 25)
TextBox1.Text = ""
textbox2.Text = (RandomClass.Next(1, 10))
textbox3.Text = (RandomClass.Next(1, 10))
symbole = CInt(Int(Rnd() * 4) + 1)
If symbole = 1 Then label2.Text = ("+")
If symbole = 2 Then label2.Text = ("-")
If symbole = 3 Then label2.Text = ("*")
If symbole = 4 Then label2.Text = ("/")
End Sub
Sub check()
If symbole = 1 Then check1()
If symbole = 2 Then check2()
If symbole = 3 Then check3()
If symbole = 4 Then check4()
End Sub
Sub Check1()
Dim variable1 As Double
Double.TryParse(TextBox1.Text, variable1)
Dim variable2 As Double
Double.TryParse(textbox2.Text, variable2)
Dim answers As Double = variable1 + variable2
If variable1 + variable2 = answers Then
Msgboxcor()
Else
If variable1 + variable2 <> answers Then Msgboxinc()
End If
End Sub
Sub check2()
Dim variable1 As Double
Double.TryParse(TextBox1.Text, variable1)
Dim variable2 As Double
Double.TryParse(textbox2.Text, variable2)
Dim answers As Double = variable1 - variable2
If variable1 - variable2 = answers Then
Msgboxcor()
Else
Msgboxinc()
End If
End Sub
Sub check3()
Dim variable1 As Double
Double.TryParse(TextBox1.Text, variable1)
Dim variable2 As Double
Double.TryParse(textbox2.Text, variable2)
Dim answers As Double = variable1 * variable2
If variable1 * variable2 = answers Then
Msgboxcor()
Else
Msgboxinc()
End If
End Sub
Sub check4()
Dim variable1 As Double
Double.TryParse(TextBox1.Text, variable1)
Dim variable2 As Double
Double.TryParse(textbox2.Text, variable2)
Dim answers As Double = variable1 * variable2
If variable1 * variable2 = answers Then
Msgboxcor()
Else
Msgboxinc()
End If
End Sub
Sub Msgboxcor()
MsgBox("Correct")
Total_Correct = +1
randomize()
End Sub
Sub Msgboxinc()
MsgBox("Incorrect")
Wrong_Answer = +1
Lifes = +1
If Lifes = 2 Then Close()
End Sub
End Class
-
May 27th, 2011, 07:49 PM
#2
Hyperactive Member
Re: Correct / Incorrect
Not completely sure what you are trying to do with this program, but...
vb Code:
Dim answers As Double = variable1 + variable2
If variable1 + variable2 = answers Then
Msgboxcor()
Else
If variable1 + variable2 <> answers Then Msgboxinc()
End If
You just set you "answers" variable = variable1 + variable2, then compaire if it is equal to variable1 + variable2...
Of course it is going to say it is true.
Thats like saying:
vb Code:
Answer = 1
If 1 = Answer then
-
May 27th, 2011, 10:04 PM
#3
Re: Correct / Incorrect
You are declaring Answers at class scope (outside of all methods), then you are declaring a different variable of the same name inside your Check methods. That's not good. What nO One stated is entirely correct, but why do you even have the Answers variable at class scope?
There are other points, though they don't directly relate to the problem.
1) You are using a Random object called RandomClass. That's good. However, you are also using the old VB6 style Rnd() method, which is totally bizarre. Get rid of the Rnd calls, the lines that are using that should be using your RandomClass object. There certainly isn't any advantage to Rnd. It is harder to use, harder to read, and requires other things that you won't be able to do (such as calling the Randomize function, which will be tricky since you have a function with that name in the class, so you would have to decorate the call to the other Randomize, but without that call, Rnd will behave poorly).
2) You have this:
Code:
Wrong_Answer = +1
Lifes = +1
If Lifes = 2 Then Close()
What you meant to write is:
Code:
Wrong_Answer += 1
Lifes += 1
If Lifes = 2 Then Close()
The way you have it, Lifes will just be set to 1, so the If statement will never be True, because Lifes will always be 1 and never anything else.
3) You clearly don't have Option Strict ON, or else parts of that code wouldn't compile, but you are doing a pretty good job of explicitly converting strings to variables already, so you should just turn Option Strict ON and fix the few errors remaining. Option Strict makes for faster code, and forces you to learn some good habits that you are mostly doing already (your use of Double.TryParse shows that).
4) One quibble about your use of Double.Tryparse is that it will return False if the string can't be converted to a number. Since you aren't checking the return value, if the user enters garbage into the two textboxes, then both variable1 and variable2 will hold 0. That might be ok, though it is likely to result in the wrong answer, and in some cases, it could lead to incorrectly showing the right answer. There is a worse problem with that if you used division, since you would be dividing by 0, but see item 5.
5) You have four different operations: +, -, *, and /. You also have four 'check' methods. The first three use +, -, and *, so I would expect that check4 would use /, but it just uses * a second time. I would guess that that one is incorrect.
My usual boring signature: Nothing
 
-
May 28th, 2011, 06:39 AM
#4
Thread Starter
Member
Re: Correct / Incorrect
Thanks for pointing out the mistakes i made with the Divide didn't notice that. and i have changed the counters to +=1, also put strict on and have now got no errors with strict.
what am i changing rnd to ? RandomClass object, would i replace all rnd's with ( RandomClass.next ) ?
something like this ? i know im not doing it right because when i launch the program no random questions appere, but im putting it up to show you what im doing because i dont know what im to put in its place :P but helps is i put it up so you can see.
vb Code:
Int(RandomClass.Next() * 25) TextBox1.Text = "" textbox2.Text = CStr((RandomClass.Next(1, 10))) textbox3.Text = CStr((RandomClass.Next(1, 10))) symbole = CInt(Int(RandomClass.Next() * 4) + 1)
also im a little confused what im doing with try.parse, what do i have to change so i can check the returned value, so its ether right or wrong ? sorry if im becoming a pain :P most of this is new to me.
Thanks for your cooperation and patience
vb Code:
Public Class Form3 Dim RandomClass As New Random Dim symbole As Integer Dim Answers As Integer Public Wrong_Answer As Integer Public Total_Correct As Integer Public Lifes As Integer Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click check() End Sub Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load lblname.Text = username randomize() End Sub Sub randomize() Int(Rnd() * 25) TextBox1.Text = "" textbox2.Text = CStr((RandomClass.Next(1, 10))) textbox3.Text = CStr((RandomClass.Next(1, 10))) symbole = CInt(Int(Rnd() * 4) + 1) If symbole = 1 Then label2.Text = ("+") If symbole = 2 Then label2.Text = ("-") If symbole = 3 Then label2.Text = ("*") If symbole = 4 Then label2.Text = ("/") End Sub Sub check() If symbole = 1 Then Check1() If symbole = 2 Then check2() If symbole = 3 Then check3() If symbole = 4 Then check4() End Sub Sub Check1() Dim variable1 As Double Double.TryParse(textbox2.Text, variable1) Dim variable2 As Double Double.TryParse(textbox3.Text, variable2) Dim answers As Double = variable1 + variable2 If Double.TryParse(TextBox1.Text, answers) Then Msgboxcor() Else If variable1 + variable2 <> answers Or False Then Msgboxinc() End If End Sub Sub check2() Dim variable1 As Double Double.TryParse(textbox2.Text, variable1) Dim variable2 As Double Double.TryParse(textbox3.Text, variable2) Dim answers As Double = variable1 - variable2 If variable1 - variable2 = answers Then Msgboxcor() Else Msgboxinc() End If End Sub Sub check3() Dim variable1 As Double Double.TryParse(textbox2.Text, variable1) Dim variable2 As Double Double.TryParse(textbox3.Text, variable2) Dim answers As Double = variable1 * variable2 If variable1 * variable2 = answers Then Msgboxcor() Else Msgboxinc() End If End Sub Sub check4() Dim variable1 As Double Double.TryParse(textbox2.Text, variable1) Dim variable2 As Double Double.TryParse(textbox3.Text, variable2) Dim answers As Double = variable1 / variable2 If variable1 / variable2 = answers Then Msgboxcor() Else Msgboxinc() End If End Sub Sub Msgboxcor() MsgBox("Correct") Total_Correct += 1 randomize() End Sub Sub Msgboxinc() MsgBox("Incorrect") Wrong_Answer += 1 Lifes += 1 If Lifes = 2 Then Close() End Sub End Class
-
May 28th, 2011, 08:56 AM
#5
Re: Correct / Incorrect
Rnd() produced a floating point value that you had to manipulate to get it into the range you wanted to work with, then convert it into an integer. That's what all the *x + n stuff was about when you used Rnd(). The Random object is superior to that, because you just tell it the range you want as arguments to the Next method:
For values from 0 through 25:
value = RandomClass.Next(26)
For values from 5 through 25:
value = RandomClass.Next(5,26)
Note that the lower value is included in the range, but the upper value is one higher than the range that you want. Also note that if the bottom of the range is 0, you don't even need to mention it, and only the upper bound is needed. Furthermore, since .Next returns an Integer, you don't have to convert it into an integer with CInt, because it already IS an integer.
As for the Double.TryParse, there are many ways you could work with this. The question you need to decide is what you want to do if the entry in the textbox is NOT a number, which means that it contains non-numeric characters, or was left empty. What you want to do in that case will determine the best solution for you. One option would look like this:
Code:
If Not Double.TryParse(TextBox1.Text, variable1) Then
Windows.Forms.MessageBox.Show("You didn't enter a number!","Invalid Number")
Return
End If
Doing something like that for each of your TryParse statements would mean that if the user clicked that button without entering valid numbers in the boxes, they would get a message, then nothing more would happen. That's the simplest possible solution. Of course, it would be better if the message indicated which textbox had bad information.
Written this way, if there was something bad in the first textbox, the second textbox wouldn't even be examined. A much more complicated alternative would be to check each textbox, noting any problems with either one, and if either had a problem, then show those problems. This would allow the user to see ALL problems at once, rather than going through each one in turn. That's probably excessive for this program, but when you are validating MANY different inputs where the user could have made many different types of errors, then it would make more sense.
Other people would take issue with my use of Return. There is a school of thought, to which I do not subscribe fully, that believes that all functions should have only one return point. If you believe that way, you could change the code such that it looked like this:
Code:
If Double.TryParse(Textbox1.Text,variable1) Then
If Double.TryParse(Textbox2.Text,variable2) Then
'All is well, the rest of your method goes here.
Else
'The second variable is bad, put up a message.
End If
Else
'The first variable is bad, put up a message.
End If
So there are options, as you can see.
My usual boring signature: Nothing
 
-
May 28th, 2011, 01:50 PM
#6
Thread Starter
Member
Re: Correct / Incorrect
thanks shaggy hiker for your help.
have made the following changes you pointed out, codes getting messy now but im sure it does that no matter what you do :P
iv made all the corrections but still after entering a correct answer it 1st pops up saying correct and then straight after it pops up the incorrect msgbox too. il put my code back up, sorry for all the hassle.
vb Code:
Public Class Form3 Dim RandomClass As New Random Dim symbole As Integer Dim Answers As Integer Public Wrong_Answer As Integer Public Total_Correct As Integer Public Lifes As Integer Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click check() End Sub Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load lblname.Text = username randomize() lblcorrect.Text = CStr(Total_Correct) lblwrong.Text = CStr(Wrong_Answer) lbllife.Text = CStr(Lifes) End Sub Sub randomize() RandomClass.Next(0, 26) TextBox1.Text = "" textbox2.Text = CStr(RandomClass.Next(1, 10)) textbox3.Text = CStr(RandomClass.Next(1, 10)) symbole = RandomClass.Next(1, 4) If symbole = 1 Then label2.Text = ("+") If symbole = 2 Then label2.Text = ("-") If symbole = 3 Then label2.Text = ("*") If symbole = 4 Then label2.Text = ("/") End Sub Sub check() If symbole = 1 Then Check1() If symbole = 2 Then check2() If symbole = 3 Then check3() If symbole = 4 Then check4() End Sub Sub Check1() Dim variable1 As Double Double.TryParse(textbox2.Text, variable1) Dim variable2 As Double Double.TryParse(textbox3.Text, variable2) Answers = CInt(variable1 + variable2) Dim Userinput As Double Double.TryParse(TextBox1.Text, Userinput) If Not Double.TryParse(TextBox1.Text, variable1) Then Windows.Forms.MessageBox.Show("Please Enter an answer to the question!", "Invalid Number") Return End If If Userinput = Answers Then Msgboxcor() Else Msgboxinc() End If End Sub Sub check2() Dim variable1 As Double Double.TryParse(textbox2.Text, variable1) Dim variable2 As Double Double.TryParse(textbox3.Text, variable2) Dim answers As Double = variable1 - variable2 answers = CInt(variable1 - variable2) Dim Userinput As Double Double.TryParse(TextBox1.Text, Userinput) If Not Double.TryParse(TextBox1.Text, variable1) Then Windows.Forms.MessageBox.Show("Please Enter an answer to the question!", "Invalid Number") Return End If If Userinput = answers Then Msgboxcor() Else Msgboxinc() End If End Sub Sub check3() Dim variable1 As Double Double.TryParse(textbox2.Text, variable1) Dim variable2 As Double Double.TryParse(textbox3.Text, variable2) Answers = CInt(variable1 * variable2) Dim Userinput As Double Double.TryParse(TextBox1.Text, Userinput) If Not Double.TryParse(TextBox1.Text, variable1) Then Windows.Forms.MessageBox.Show("Please Enter an answer to the question!", "Invalid Number") Return End If If Userinput = Answers Then Msgboxcor() Else Msgboxinc() End If End Sub Sub check4() Dim variable1 As Double Double.TryParse(textbox2.Text, variable1) Dim variable2 As Double Double.TryParse(textbox3.Text, variable2) Answers = CInt(variable1 / variable2) Dim Userinput As Double Double.TryParse(TextBox1.Text, Userinput) If Not Double.TryParse(TextBox1.Text, variable1) Then Windows.Forms.MessageBox.Show("Please Enter an answer to the question!", "Invalid Number") Return End If If Userinput = Answers Then Msgboxcor() Else Msgboxinc() End If End Sub Sub Msgboxcor() MsgBox("Correct") Total_Correct += 1 Lifes = 0 randomize() End Sub Sub Msgboxinc() MsgBox("Incorrect") Wrong_Answer += 1 Lifes += 1 If Lifes = 2 Then Close() End Sub End Class
-
May 28th, 2011, 03:21 PM
#7
Re: Correct / Incorrect
Time to do a bit of debugging. Put a breakpoint on the call to check(), then step through the code. Something odd is happening if you are following both branches of the If statement, but I'm not seeing it. Stepping through the code will show it.
My usual boring signature: Nothing
 
-
May 28th, 2011, 04:00 PM
#8
Hyperactive Member
Re: Correct / Incorrect
vb Code:
Sub Msgboxcor()
MsgBox("Correct")
Total_Correct += 1
Lifes = 0
randomize()
End Sub
Maybe it's becuase you are calling your randomize sub from within your msgboxcor sub. That is weird because it shouldn't re-evaluate the variables within the Check sub.
Try taking out the randomize() from you msgboxcor sub (you don't have it in the msgboxinc sub) and see if that works.
If that is the problem, but you want to keep it there, then setup your If statement so that it ends the Check sub after it runs the msgboxcor sub, that way it doesn't evaluate the other portion of the If statement.
-
May 28th, 2011, 04:10 PM
#9
Thread Starter
Member
Re: Correct / Incorrect
if i take it out and answer a question correctly it wont go back to randomize - it wont put a new question up, the reason i dont have it in msgboxinc sub is because if they get it wrong it leaves the save question on until they get it right but if they get it wrong 2 times the game ends.
if i take it out of the msgboxcor sub then what do i use so it puts a new random question after its answered correctly ?
-
May 28th, 2011, 04:14 PM
#10
Hyperactive Member
Re: Correct / Incorrect
I realized that after I posted it.
If it didn't give you both msgs when you took it out, the I would reverse your if statement, evaluate if it is incorrect first, then check if it is correct
-
May 28th, 2011, 04:28 PM
#11
Thread Starter
Member
Re: Correct / Incorrect
Yeah taking the randomize() out i dont appere to get any of the wrong messages. so i should start by evaluating whether its wrong first, by that would you of put >>
vb Code:
If Userinput <> Answers Then Msgboxinc() else Msgboxcor() End if
if i do that the whole of my code on form3 has the blue line under everything. so im not sure what to do in place of <> answers.
-
May 28th, 2011, 04:57 PM
#12
Hyperactive Member
Re: Correct / Incorrect
I am on my phone right now, so I can't test,
But you an try it like:
Code:
If not userinput = answer then
Last edited by nO_OnE; May 28th, 2011 at 05:03 PM.
-
May 28th, 2011, 05:06 PM
#13
Thread Starter
Member
Re: Correct / Incorrect
right that goes in without the blue lines but i still get both correct and incorrect msg box pop up occasinaly. from going through brakepoint at the check sub, that appears to be where its making the problem, running through it step by step it goes to one check says correct then press f10 then the message pops up because it goes to a new check if that makes sence. kinda hard to explain xD
im in no rush so dont worry :P
-
May 28th, 2011, 05:20 PM
#14
Re: Correct / Incorrect
Don't use F10, that will hide the problem. You are going about it the right way, but the solution is not going to be what you expect. Using F11 will step you through each line, including each sub. That should show you something unusual.
My usual boring signature: Nothing
 
-
May 28th, 2011, 05:26 PM
#15
Thread Starter
Member
Re: Correct / Incorrect
going through with F11 has shown me what is making it do it.
it appears to be that some times after answering a question then it goes back to randomizing a new one then skips the part where a user enters an answer and goes straight to saying that no answer was input / incorrect, it dosent go to the stage where it allows the user to input the answer.
-
May 28th, 2011, 05:33 PM
#16
Re: Correct / Incorrect
I'm not sure what you mean by that, but I just noticed that you are getting a random number with the line .Next(1,4)
The 4 is one above the maximum value, so that will return the values 1-3, whereas you want the values 1-4, so that should be .Next(1,5)
My usual boring signature: Nothing
 
-
May 28th, 2011, 05:49 PM
#17
Thread Starter
Member
Re: Correct / Incorrect
i did not notice i had put 4, thanks for pointing that out. what i will do is make a video of what happens going through the cycles, that will help show what im seeing :P
-
May 28th, 2011, 06:47 PM
#18
Thread Starter
Member
Re: Correct / Incorrect
Scratch that idea, my net dosnt upload as fast as i thought it would, so on Wednesday im taking what i have to college and going to show my issue to my lecturer and see if he can fathom out what is happening.
thanks so much though for your help, from where i started im a hell of a lot further than i would be without the help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|