# Random Number Guessing Game Visual Basic • ### Question

• I don't understand where I am doing something wrong. The instructions is to create a random application that generates a random number in the range of 1 through 100. If it's too low state "too low, try again" and the same for if it's too high.

(I'm using visual basic)

So I declared my variables and wrote a code for the procedure. I declared x as my integer. Any advice on how to correct it?
Thanks for all help

intNum = rand.Next(100) + 1
For
For x > intNum
MsgBox("Too high, try again")
Next
For x < intNum
MsgBox("Too low, try again")
Next
For x = intNum
MsgBox("You guessed right!")
Next
Next
Saturday, November 24, 2012 9:27 PM

• Hi Fir93,

I would suggest use If statement to reach it.

```    Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
Dim inputNumber As Integer
If Integer.TryParse(TextBox1.Text, inputNumber) AndAlso e.KeyCode = Keys.Enter Then
If inputNumber > rdmNum Then
MsgBox("Too high, try again")
ElseIf inputNumber < rdmNum Then
MsgBox("Too low, try again")
ElseIf inputNumber = rdmNum Then
MsgBox("You guessed right!")
End If
Else

End If
End Sub```

Hope this helps.

Best regards,

Monday, November 26, 2012 7:23 AM

### All replies

• Hi Fir93,

I would suggest use If statement to reach it.

```    Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
Dim inputNumber As Integer
If Integer.TryParse(TextBox1.Text, inputNumber) AndAlso e.KeyCode = Keys.Enter Then
If inputNumber > rdmNum Then
MsgBox("Too high, try again")
ElseIf inputNumber < rdmNum Then
MsgBox("Too low, try again")
ElseIf inputNumber = rdmNum Then
MsgBox("You guessed right!")
End If
Else

End If
End Sub```

Hope this helps.

Best regards,

Monday, November 26, 2012 7:23 AM
• Hi Fir93,

I would suggest use code  statement to reach it.

`   ''--Add TextBox1 and textBox2 and change names at numRandomNumber1,numRandomNumber2Option Explicit  '-   Public StartOrEnd As BooleanPrivate Sub cmdClose_Click()  '---  Me.StartOrEnd = False  '-  Unload Me  '---End SubPrivate Sub cmdOk_Click()  '---  Me.StartOrEnd = True  '-  Unload Me  '---End SubPrivate Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)  '---  Select Case KeyCode    Case VBRUN.vbKeyEscape      Me.StartOrEnd = False      Unload Me  End Select  '---End SubPrivate Sub Form_Load()  Randomize  Me.Label1(2).Caption = Int((999999 * Rnd) + 100000)  Me.Label1(3).Caption = Int((999999 * Rnd) + 100000)End SubPrivate Sub numRandomNumber1_KeyPress(KeyAscii As Integer)  Select Case KeyAscii    Case vbKeyReturn      SendKeys vbTab  End SelectEnd SubPrivate Sub numRandomNumber1_KeyUp(KeyCode As Integer, Shift As Integer)  CompareRandomNumber _      RndFrom1:=Val(Me.Label1(2).Caption), _      RndTo1:=Val(Me.numRandomNumber1.Text), _      RndFrom2:=Val(Me.Label1(3).Caption), _      RndTo2:=Val(Me.numRandomNumber2.Text)End SubPrivate Sub numRandomNumber2_KeyPress(KeyAscii As Integer)  Select Case KeyAscii    Case vbKeyReturn      SendKeys vbTab  End Select  '---End SubPublic Static Sub CompareRandomNumber( _        RndFrom1 As Long, RndTo1 As Long, _        RndFrom2 As Long, RndTo2 As Long)  If (RndFrom1 = RndTo1) And (RndFrom2 = RndTo2) Then    Me.cmdOk.Enabled = True  Else    Me.cmdOk.Enabled = False  End IfEnd SubPrivate Sub numRandomNumber2_KeyUp(KeyCode As Integer, Shift As Integer)  CompareRandomNumber _    RndFrom1:=Val(Me.Label1(2).Caption), _    RndTo1:=Val(Me.numRandomNumber1.Text), _    RndFrom2:=Val(Me.Label1(3).Caption), _    RndTo2:=Val(Me.numRandomNumber2.Text)End Sub`

Hope this helps.

Best regards,

Wednesday, December 19, 2012 1:00 PM