# For Next Loop, Number Guessing Game

Page 1 of 1

## 6 Replies - 9147 Views - Last Post: 17 March 2010 - 07:33 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=145745&amp;s=b94c46b04244d7a362ca0b1a19f573ee&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 hugoriffic

Reputation: 0
• 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

MessageBox.Show("You Win")
End

MessageBox.Show("Too High", "Try Again...")

MessageBox.Show("Too Low", "Try Again...")

ElseIf intCount = intNumGuess Then

End If

Next
End Sub
```

Is This A Good Question/Topic? 0

## Replies To: For Next Loop, Number Guessing Game

### #2 LeisureProgrammer

• N00by VB.NET Developer

Reputation: 110
• 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

MessageBox.Show("You Win")
End

MessageBox.Show("Too High", "Try Again...")

MessageBox.Show("Too Low", "Try Again...")

ElseIf intCount = intNumGuess Then

End If

```

To:

```If intCount < intNumGuess Then

MessageBox.Show("You Win")
End

MessageBox.Show("Too High", "Try Again...")

MessageBox.Show("Too Low", "Try Again...")

ElseIf intCount = intNumGuess Then

End If
End If

```

### #3 hugoriffic

Reputation: 0
• 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
```

### #4 hugoriffic

Reputation: 0
• 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

### #5 LeisureProgrammer

• N00by VB.NET Developer

Reputation: 110
• 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

MessageBox.Show("You Win")
End

MessageBox.Show("Too High", "Try Again...")

MessageBox.Show("Too Low", "Try Again...")

End If
ElseIf intCount = intNumGuess Then

End If

```

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

### #6 hugoriffic

Reputation: 0
• 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??

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
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]