Results 1 to 6 of 6

Thread: Random Numbers Game

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    8

    Random Numbers Game

    Hello Everyone! I was hoping if you can please take a look at my code below and let me know what I need to do in order for the application to work correctly. Here's my assignment:

    Code an application that allows the user 10 chances to guess a random generated by the computer. The random number should be an integer from 1 through 50, inclusive. Each time the user makes an incorrect guess, the application should display a message that tells the user either to guess a higher number or to guess a lower number. When the user guesses the random number, the application should display a "Congratulations!" message. However, if the user is not able to guess the random number after 10 tries, the application should display the random number in a message. This what the GUI looks like:
    Name:  GUIRandomNumbers.jpg
Views: 5463
Size:  16.2 KB

    Here's my code:
    Option Explicit On
    Option Strict On
    Option Infer Off

    Public Class frmMain

    Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
    Me.Close()
    End Sub

    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
    Dim intRandom As Integer
    Dim randomGenerator As New Random
    intRandom = randomGenerator.Next(1, 51)
    Const strPROMPT As String =
    "Please enter your number. " &
    ControlChars.NewLine &
    "Click Cancel or leave blank to end."
    Const strTITLE As String = "Random Number"
    Dim intGuess As Integer
    Dim intCount As Integer
    Static strDisplayMessage As String

    ' repeat as long as the user enters a number
    Do While intCount <= 10

    ' get first first number
    strDisplayMessage = InputBox(strPROMPT, strTITLE)

    'update the counter
    intCount += 1
    If strDisplayMessage <> String.Empty Then
    'convert guess amount to a number
    Integer.TryParse(strDisplayMessage, intGuess)
    End If

    If intGuess = intRandom Then
    MessageBox.Show("Congratulations", "Random Number", MessageBoxButtons.OK)
    Else
    MessageBox.Show("Please guess a higher number.", "Random Number", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    Else If
    MessageBox.Show("Please guess a higher number.", "Random Number", MessageBoxButtons.OK)
    End If

    Loop

    End Sub
    End Class

    My loop is not working correctly.... any help is greatly appreciated.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,008

    Re: Random Numbers Game

    Firstly, please use formatting tags when posting code snippets as it makes reading the code much easier. You can type them yourself or use the buttons provided on the advanced editor.

    Secondly, rather than just saying that your loop is not working, please describe exactly what you expect to happen and what actually does happen. We may well be able to work it out for ourselves but we shouldn't really have to. If you provide a full and clear description of the issue then we can hone in on the issue immediately.

  3. #3
    Fanatic Member
    Join Date
    Oct 2011
    Location
    Sydney, Australia
    Posts
    756

    Re: Random Numbers Game

    this IF statement makes no sense to me
    Code:
    If intGuess = intRandom Then
    MessageBox.Show("Congratulations", "Random Number", MessageBoxButtons.OK)
    Else 
    MessageBox.Show("Please guess a higher number.", "Random Number", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    Else If
    MessageBox.Show("Please guess a higher number.", "Random Number", MessageBoxButtons.OK)
    End If
    try taking out the last elseif like so
    Code:
    If intGuess = intRandom Then
    MessageBox.Show("Congratulations", "Random Number", MessageBoxButtons.OK)
    Else 
    MessageBox.Show("Please guess a higher number.", "Random Number", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    End If

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    8

    Re: Random Numbers Game

    Option Explicit On
    Option Strict On
    Option Infer Off

    Public Class frmMain

    Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
    Me.Close()
    End Sub

    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
    Dim intRandom As Integer
    Dim randomGenerator As New Random
    intRandom = randomGenerator.Next(1, 51)
    Const strPROMPT As String =
    "Please enter your number."
    Const strTITLE As String = "Random Number"
    Dim intGuess As Integer
    Dim intCount As Integer
    Static strDisplayMessage As String

    ' repeat as long as the user enters a number
    Do While intCount <= 10

    ' get first first number
    strDisplayMessage = InputBox(strPROMPT, strTITLE)

    'update the counter
    intCount += 1
    If strDisplayMessage <> String.Empty Then
    'convert guess amount to a number
    Integer.TryParse(strDisplayMessage, intGuess)
    End If

    If intGuess = intRandom Then
    MessageBox.Show("Congratulations", "Random Number", MessageBoxButtons.OK)
    Else
    MessageBox.Show("Please guess a higher number.", "Random Number", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

    End If

    Loop

    End Sub
    End Class


    Thank you for the quick reply. Ideally, I'd like for a user to click on the "Start Game" button and to enter a number using the Random Input Box function and allows 10 chances to guess a random number generated by the computer. Each time the user makes an incorrect guess, it will display a message to guess higher or a lower number. It will display a congratulations message, if the user guesses correctly. After the 10 tries, the application should display the random in a message.

    In my code, the user enters the number and it keeps displaying the message to guess a higher number and it loops 10 times. Can you please provide any suggestions what I need to change in my loop statement to display either to guess a higher number or lower number or congrats message? Thanks in advance for your help.

  5. #5
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,252

    Re: Random Numbers Game

    Quote Originally Posted by amarlv07 View Post
    Code:
        If intGuess = intRandom Then
            MessageBox.Show("Congratulations", "Random Number", MessageBoxButtons.OK)
        Else 
            MessageBox.Show("Please guess a higher number.", "Random Number", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Else If
            MessageBox.Show("Please guess a higher number.", "Random Number", MessageBoxButtons.OK)
        End If
    Loop
    You should be using ElseIf to tests the second (or more) condition, when there is only one possible condition left then you can simply use Else,..
    Code:
    If A = B then
    ' do something
    elseif A > B Then
    ' do something
    else ' only other possible condition is, A is less then B
    ' do something
    end if
    Don't forget if guess is correct then you'll most likely want to exit the do-loop so user can start a new game...

    Code:
    If intGuess = intRandom Then
       MessageBox.Show("Congratulations")
       EXIT DO
    ElseIf intGuess < intRandom Then 
       MessageBox.Show("Please guess a higher number.")
    Else ' intGuess is greater then intRandom
       MessageBox.Show("Please guess a lower number.")
    End If

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    8

    Re: Random Numbers Game

    Thank you very much for all of your help!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width