By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,730 Members | 1,546 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,730 IT Pros & Developers. It's quick & easy.

Equality Help...Can Figure it out

P: n/a
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

Oct 4 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
It is because you never set randomNumber to any value. It defaults to 0.

"ml*********@aol.com" wrote:
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

Oct 5 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.