|
-
Jun 29th, 2012, 04:51 AM
#1
Thread Starter
Junior Member
Some Conditions are not executing
Code:
Dim userguess As Integer
Dim compnumber As Integer
Dim usercount As Integer
Dim TestNumber As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Randomize()
compnumber = Int((100 * Rnd()) + 1) '// Generate random value between 1 and 100.
btnTry.Visible = False
End Sub
Private Sub btnGuess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuess.Click
TestNumber = txtguess.Text
If IsNumeric(TestNumber) = True Then ' check if string is a number value
userguess = Val(TestNumber)
If userguess > compnumber Then
txtmessage.Text = "Go Lower"
Me.BackColor = Color.Red
ElseIf userguess < compnumber Then
txtmessage.Text = "Go Higher"
Me.BackColor = Color.Blue
ElseIf userguess = compnumber Then
txtmessage.Text = "CORRECT"
Me.BackColor = Color.Yellow
End If
Else
MsgBox(" You did not enter a numerical value ") ' if string value entered is a letter
End If
txtguess.Text = ""
txtguess.Focus()
usercount = usercount + 1
txtcount.Text = usercount
If userguess = compnumber And usercount < 5 Then
MsgBox(" Either you know the secret, or you got lucky")
btnTry.Visible = True
ElseIf userguess = compnumber And usercount >= 5 & 6 & 7 Then
MsgBox(" Aha, do you know the secret?")
btnTry.Visible = True
ElseIf userguess = compnumber And usercount >= 8 & 9 & 10 Then
MsgBox(" Try to do better next time")
btnTry.Visible = True
End If
If usercount >= 10 Then
btnGuess.Enabled = False
MsgBox(" You have taken too many guesses buddy")
btnTry.Visible = True
End If
lstGuess.Items.Add(usercount & " " & userguess)
End Sub
Private Sub BtnHowToPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnHowToPlay.Click
MsgBox("Type a number between 0 and 100 into the box labeled Type Guess Here. The program will _generate a random number and will tell you if you've gone to high or to low, keep guessing until you are CORRECT! but you only have 10 guesses.")
End Sub
Private Sub btnTry_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTry.Click
btnGuess.Enabled = True
txtguess.Text = ""
Me.lstGuess.Text = " "
txtmessage.Text = ""
txtcount.Text = " "
usercount = 0
Randomize()
compnumber = (Rnd() * 100)
usercount = 0
lstGuess.Items.Clear()
Me.BackColor = Color.Empty
lstGuess.Items.Add("Guess #" & " " & "Number")
txtguess.Focus()
End Sub
Private Sub Form_Load()
Randomize()
compnumber = (Rnd() * 100)
usercount = 0
End Sub
End Class
The conditions that display
Code:
If userguess = compnumber And usercount < 5 Then
MsgBox(" Either you know the secret, or you got lucky")
btnTry.Visible = True
ElseIf userguess = compnumber And usercount >= 5 & 6 & 7 Then
MsgBox(" Aha, do you know the secret?")
btnTry.Visible = True
ElseIf userguess = compnumber And usercount >= 8 & 9 & 10 Then
MsgBox(" Try to do better next time")
btnTry.Visible = True
are not executing with my program and need help figuring out why.
-
Jun 29th, 2012, 05:04 AM
#2
Re: Some Conditions are not executing
For one thing 5 & 6 & 7 is really 567. Is that what you meant? Same with 8 & 9 & 10 - that is 8910.
-
Jun 29th, 2012, 10:05 AM
#3
Thread Starter
Junior Member
Re: Some Conditions are not executing
N this is not what I meant. I want 5 6 7 8 and 9 ad separate values to compare the counter with.
-
Jun 29th, 2012, 10:11 AM
#4
Junior Member
Re: Some Conditions are not executing
If you want "greater than 5,6, & 7 don't you want "greater than 7" ?
Or do you want 5,6, OR 7, in which case I'd suggest a Case statement.
-
Jun 29th, 2012, 10:22 AM
#5
Thread Starter
Junior Member
Re: Some Conditions are not executing
I want the condition to apply when it is greater than or equal to 5 and less than on equal to 7 . Greater than or 7 and less than 10
-
Jun 29th, 2012, 10:25 AM
#6
Junior Member
Re: Some Conditions are not executing
So write it that way...
Try a case statement.
Select case usercount
Case 0 to 5
.
.
Case 5 to 7
.
.
Case 7 to 10
.
.
http://msdn.microsoft.com/en-us/libr...(v=vs.80).aspx
-
Jun 29th, 2012, 10:28 AM
#7
Re: Some Conditions are not executing
try this:
Code:
ElseIf userguess = compnumber And usercount >= 8 Then
MsgBox(" Try to do better next time")
btnTry.Visible = True
ElseIf userguess = compnumber And usercount >= 5 Then
MsgBox(" Aha, do you know the secret?")
btnTry.Visible = True
If it's equal to or greater than 8, that fulfills the 8,9, or 10 condition.... otherwise it continues to the next if where it compares it to equal to or greater than 5 .....
so if the user count is 7 .... it's not greater than 8, and so falls to the second one, where it is greater than 5...
-tg
-
Jun 29th, 2012, 10:29 AM
#8
Junior Member
Re: Some Conditions are not executing
Ya I started writing it the way technome did but was too lazy to write the explanation so I suggested Case
-
Jun 29th, 2012, 10:56 AM
#9
Re: Some Conditions are not executing
since I'm pedantic and anal about certain coding techniques... I'd like to point out that the case statement as posted is incorrect... if the value is 7... which branch will it take? They shouldn't overlap... it should be 0 to 4, 5 to 7, 8 to 10.... the overlap "works" but only because it will take the first matching branch.,.. but form a readability & maintenance view, it's not so clear and someone doing a cursory glance at it may not distinguish the overlap and trace through the wrong path.
-tg
-
Jun 29th, 2012, 11:01 AM
#10
Junior Member
Re: Some Conditions are not executing
Ya you're techgnome, for 7 there's 2 matches and it happens to pick the right one because of how it's laid out, but ya, shoulda been 8 to 10.
Being pedantic is good sometimes .
Tags for this Thread
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
|