M
mlarsen1982
Hello everyone
I am trying to write this application that picks a random number
between 1-1000, and prompts the user to attempt to enter a number to
try to guess it. Depending on how far or close the number guessed is
to random number created by the program, the background will turn blue
for colder and red for warmer also displaying a label telling the user
this. If you guess right it turns green and says correct...for some
reason it only accepts '0' as correct answer. I think there is
something wrong with the equality statements???
any help is really appreciated...here is my code
Public Class Form1
Dim randomObject As New Random()
Dim randomNumber As Integer
Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
txtInput.Enabled = True
For i As Integer = 1 To 1
'randomNumber = randomObject.Next(1, 1000)
i = randomNumber
Next
End Sub
Public Sub txtInput_KeyDown(ByVal sender As System.Object, ByVal e
As System.Windows.Forms.KeyEventArgs) Handles txtInput.KeyDown
'Dim array As Integer() = New Integer(999) {}
Dim number As Integer
If e.KeyCode = Keys.Enter Then
number = Convert.ToInt32(txtInput.Text)
'For i As Integer = 0 To array.GetUpperBound(0)
'array(i) = RandomNumber()
'Next
If number = randomNumber Then
MessageBox.Show("Correct!")
Me.BackColor = Color.Green
txtInput.Enabled = False
End If
If number > randomNumber Then
lblHighLow.Text = "Too High"
Me.BackColor = Color.Red
End If
If number < randomNumber Then
lblHighLow.Text = "Too Low"
Me.BackColor = Color.Blue
End If
End If
End Sub
I am trying to write this application that picks a random number
between 1-1000, and prompts the user to attempt to enter a number to
try to guess it. Depending on how far or close the number guessed is
to random number created by the program, the background will turn blue
for colder and red for warmer also displaying a label telling the user
this. If you guess right it turns green and says correct...for some
reason it only accepts '0' as correct answer. I think there is
something wrong with the equality statements???
any help is really appreciated...here is my code
Public Class Form1
Dim randomObject As New Random()
Dim randomNumber As Integer
Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
txtInput.Enabled = True
For i As Integer = 1 To 1
'randomNumber = randomObject.Next(1, 1000)
i = randomNumber
Next
End Sub
Public Sub txtInput_KeyDown(ByVal sender As System.Object, ByVal e
As System.Windows.Forms.KeyEventArgs) Handles txtInput.KeyDown
'Dim array As Integer() = New Integer(999) {}
Dim number As Integer
If e.KeyCode = Keys.Enter Then
number = Convert.ToInt32(txtInput.Text)
'For i As Integer = 0 To array.GetUpperBound(0)
'array(i) = RandomNumber()
'Next
If number = randomNumber Then
MessageBox.Show("Correct!")
Me.BackColor = Color.Green
txtInput.Enabled = False
End If
If number > randomNumber Then
lblHighLow.Text = "Too High"
Me.BackColor = Color.Red
End If
If number < randomNumber Then
lblHighLow.Text = "Too Low"
Me.BackColor = Color.Blue
End If
End If
End Sub