6 Replies - 8951 Views - Last Post: 17 March 2010 - 07:33 AM Rate Topic: -----

#1 hugoriffic  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 185
  • Joined: 16-September 09

For Next Loop, Number Guessing Game

Posted 15 December 2009 - 07:54 PM

Looking for a bit of help here. I know my If...Then...ElseIf statement is somehow off, but not sure how. Could someone point me in the right direction?? Thanks.

I know I am almost there but need a nudge in the right direction.

 Private Sub btnBegin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBegin.Click
		Dim intCount As Integer  'keeps running total of loops (increments)
		Dim intNumGuess As Integer = Val(txtNumGuess.Text) 'number of user assigned guesses (text box)
		Dim intUserGuess As Integer 'actual number the user inputs as guesses
		Dim intAnswer As Integer 'random number assigned to be guessed
		Dim intMin As Integer  'low number of range
		Dim intMax As Integer  'high number of range

		'Dim intRandomNumber As Integer

		intMax = Val(txtMax.Text)
		intMin = Val(txtMin.Text)


		If Not IsNumeric(txtMin.Text) Then
			MessageBox.Show("Please Enter a Valid Number!", "Error", MessageBoxButtons.OK)
			txtMin.SelectAll()
			txtMin.Focus()
			Return
		End If

		If Not IsNumeric(txtMax.Text) Then
			MessageBox.Show("Please Enter a Valid Number!", "Error", MessageBoxButtons.OK)
			txtMax.SelectAll()
			txtMax.Focus()
			Return
		End If

		If Not IsNumeric(txtNumGuess.Text) Then
			MessageBox.Show("Please Enter a Valid Number!", "Error", MessageBoxButtons.OK)
			txtNumGuess.SelectAll()
			txtNumGuess.Focus()
			Return
		End If


		Dim RandomNumber As New Random
		intAnswer = RandomNumber.Next(intMin, intMax + 1)

		For intCount = 1 To intNumGuess

			intUserGuess = InputBox("Enter Your Guess", "This is guess number " & intCount.ToString)

			If intCount < intNumGuess Then

			ElseIf intUserGuess = intAnswer Then
				MessageBox.Show("You Win")
				End

			ElseIf intUserGuess > intAnswer Then
				MessageBox.Show("Too High", "Try Again...")

			ElseIf intUserGuess < intAnswer Then
				MessageBox.Show("Too Low", "Try Again...")

			ElseIf intCount = intNumGuess Then
				MessageBox.Show("Sorry! Too Many Guesses, the answer was " & intAnswer)

			End If

		Next
	End Sub


Is This A Good Question/Topic? 0
  • +

Replies To: For Next Loop, Number Guessing Game

#2 LeisureProgrammer  Icon User is offline

  • N00by VB.NET Developer
  • member icon

Reputation: 110
  • View blog
  • Posts: 588
  • Joined: 06-September 09

Re: For Next Loop, Number Guessing Game

Posted 15 December 2009 - 08:17 PM

Be careful with your if's and ElseIf's... Change this:
 If intCount < intNumGuess Then 

			ElseIf intUserGuess = intAnswer Then
				MessageBox.Show("You Win")
				End

			ElseIf intUserGuess > intAnswer Then
				MessageBox.Show("Too High", "Try Again...")

			ElseIf intUserGuess < intAnswer Then
				MessageBox.Show("Too Low", "Try Again...")

			ElseIf intCount = intNumGuess Then
				MessageBox.Show("Sorry! Too Many Guesses, the answer was " & intAnswer)

			End If




To:

If intCount < intNumGuess Then

				If intUserGuess = intAnswer Then
					MessageBox.Show("You Win")
					End

				ElseIf intUserGuess > intAnswer Then
					MessageBox.Show("Too High", "Try Again...")

				ElseIf intUserGuess < intAnswer Then
					MessageBox.Show("Too Low", "Try Again...")

				ElseIf intCount = intNumGuess Then
					MessageBox.Show("Sorry! Too Many Guesses, the answer was " & intAnswer)

				End If
			End If


Was This Post Helpful? 0
  • +
  • -

#3 hugoriffic  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 185
  • Joined: 16-September 09

Re: For Next Loop, Number Guessing Game

Posted 15 December 2009 - 08:24 PM

It still skips over this section of code if I enter the third attempt as a wrong number.

 ElseIf intCount = intNumGuess Then
				MessageBox.Show("Sorry! Too Many Guesses, the answer was " & intAnswer)

Was This Post Helpful? 0
  • +
  • -

#4 hugoriffic  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 185
  • Joined: 16-September 09

Re: For Next Loop, Number Guessing Game

Posted 15 December 2009 - 08:31 PM

Also, is this declaration necessary??

 Dim intNumGuess As Integer = Val(txtNumGuess.Text)

This post has been edited by hugoriffic: 15 December 2009 - 08:32 PM

Was This Post Helpful? 0
  • +
  • -

#5 LeisureProgrammer  Icon User is offline

  • N00by VB.NET Developer
  • member icon

Reputation: 110
  • View blog
  • Posts: 588
  • Joined: 06-September 09

Re: For Next Loop, Number Guessing Game

Posted 15 December 2009 - 08:34 PM

Quote

Also, is this declaration necessary??

Of course it is.

Quote

It still skips over this section of code if I enter the third attempt as a wrong number.

It skips over that because you(or maybe it was me) told it to skip over the entire loop if intCount was less than intNumGuess.

To avoid that:
 If intCount < intNumGuess Then

				If intUserGuess = intAnswer Then
					MessageBox.Show("You Win")
					End

				ElseIf intUserGuess > intAnswer Then
					MessageBox.Show("Too High", "Try Again...")

				ElseIf intUserGuess < intAnswer Then
					MessageBox.Show("Too Low", "Try Again...")

				
				End If
			ElseIf intCount = intNumGuess Then
				MessageBox.Show("Sorry! Too Many Guesses, the answer was " & intAnswer)

			End If


This post has been edited by LeisureProgrammer: 15 December 2009 - 08:36 PM

Was This Post Helpful? 1

#6 hugoriffic  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 185
  • Joined: 16-September 09

Re: For Next Loop, Number Guessing Game

Posted 15 December 2009 - 08:42 PM

LeisureProgrammer,
Thank you. I appreciate the help.
The reason I asked about the declaration was due to the structure of it. Wasn't 100% certain if the "= Val(txtNumGuess.Text)" was needed. I can see how it is now. Needs to be assigned.
And, I have had the "intCount < intNumGuess" as "intCount <= intNumGuess" and "intCount < intNumGuess +1" in the past and neither of those worked either. Might have been the other issues with my code??
Was This Post Helpful? 0
  • +
  • -

#7 Guest_adam*


Reputation:

Re: For Next Loop, Number Guessing Game

Posted 17 March 2010 - 07:33 AM

Hi I have to do a number guessing game for school. I have almost all of it done except the part where if the user enters the same guess twice he/she should get a msg saying that they guessed that number already. And the guess don't count towards the max number of guesses(10). I think I would have to use an array. Here is my code so far. Can someone please help me figure out please.
[code]
Private Sub btnGuess_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGuess.Click
Dim guess As Integer = txtinput.Text
ReDim Preserve aryGuess(counter)
aryGuess(counter) = (txtinput.Text)
Dim n As Integer
For n = 0 To (aryGuess.Length - 1)
If guess = aryGuess(n) Then
lblResult.Text = "You already guessed that number please try again!"
End If
Next
If guess > numbertoguess Then lblResult.Text = "The Number You Entered Is To High, Please Try Again"
txtinput.Clear()
If guess < numbertoguess Then lblResult.Text = "The Number You Entered Is To Low, Please Try Again"
txtinput.Clear()
counter = counter + 1


lblNumGuesses.Text = counter

If counter >= 10 Then
lblResult.Text = "Sorry you used all your guesses. Please use the reset button to start again"
btnGuess.Enabled = False
Exit Sub

Else
MsgBox("Please Enter A Number From 1 to 100")
txtinput.Clear()

End If


End Sub
[code]
Was This Post Helpful? 0

Page 1 of 1