Welcome to Dream.In.Code
Become a VB Expert!

Join 149,480 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,449 people online right now. Registration is fast and FREE... Join Now!




Random Number guessing solution

 
Reply to this topicStart new topic

Random Number guessing solution, Only partly working

dvine2us
26 Apr, 2007 - 12:17 PM
Post #1

New D.I.C Head
*

Joined: 26 Apr, 2007
Posts: 1


My Contributions
I have searched an found this type (similar) question asked, however, I have written the code and it is only half way working.

The project is a Random Number guessing solution.
The project call for a solution that will generate random #'s from 1-50, and allows the User to guess what the number is 10x's before it the solution displays a message box that tells the user what the number was. The solution also is required to display a message box either telling the user to guess higher or lower, or if the user guesses correct a message displays - "you Win".

Like I said I have it written and it is partly working. the probelm is it gets "stuck after the first attempt (message box will not close)and only repeats 5 times instead of 10.

I searched all night last night, found some help here, but I need to have this done by the Morning or take a partial score for homework assignment and leave it as is.

CODE
    


Private Sub CheckButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckButton1.Click
        ' Generates a random number as stores it in a variable named computerchoice
    
        Dim computerchoice As Integer
        Dim randomGenerator As New Random
        computerchoice = randomGenerator.Next(1, 50)
        Dim userguess As Integer
        Dim guessCount As Integer
        For guessCount = 1 To 10 Step 1

            Integer.TryParse(Me.xGuessTextBox1.Text, userguess)

            ' If the User Guess matches the computer choice message box displays "You Win"

            If userguess = computerchoice Then
                MessageBox.Show("You Win", "Congratulations", MessageBoxButtons.OK)

            End If

            ' If the user Guess is lower than the computer choice
            If userguess < computerchoice Then
                MessageBox.Show("Guess Higher", "Guess Again", MessageBoxButtons.OKCancel)
                guessCount = guessCount + 1
                xGuessTextBox1.Text = String.Empty 'userguess.ToString

            End If
            If userguess > computerchoice Then

                MessageBox.Show("Guess Lower", "Guess Again", MessageBoxButtons.OKCancel)
                guessCount = guessCount + 1
                xGuessTextBox1.Text = String.Empty ' userguess.ToString

            End If
        Next guessCount
        MessageBox.Show(Convert.ToString(computerchoice), "The Number Was")

        xGuessTextBox1.Text = userguess.ToString


    End Sub


User is offlineProfile CardPM
+Quote Post

melonchollie540
RE: Random Number Guessing Solution
26 Apr, 2007 - 10:29 PM
Post #2

New D.I.C Head
*

Joined: 6 Feb, 2007
Posts: 29


My Contributions
From what I understand, the program works by the user entering their guess into a text box, and then they click a button which sets off the code below.
What you need is a static variable in the line of code that starts at 0 and goes to 10.

Another question is that you claim the message box is getting stuck on the first one. What do you mean that it won't go away, like does it just sit there or does it go away and then immediately come back up?
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 04:21PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month