CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2001
    Posts
    1

    Guess The Number Game

    I am working on my homework to create Guess The Nubmer game and I am having ahard time programming. Can some help me please??

    Thanks
    [email protected]


  2. #2
    Join Date
    Jan 2001
    Posts
    165

    Re: Guess The Number Game

    I'm not sure what game you are thinking of but I will give a go at it.

    If you would like to randomly pick a number from 1 to 100 and have the user try to guess this number with every guess telling the user whether the number is higher or lower than the guessed number you would go about it like this:

    option Explicit
    private nAnswer as Integer

    private Sub Command1_Click()
    static nTries as Integer
    If IsNumeric(Text1.Text) then
    If Text1.Text > 100 Or Text1.Text < 1 then
    MsgBox "Number out of range. Must be between 1-100."
    ElseIf Int(Text1.Text) <> Text1.Text then
    MsgBox "Only enter integers"
    else
    nTries = nTries + 1
    If Text1.Text = nAnswer then
    MsgBox "You got it!" & vbNewLine & _
    "It took you " & nTries & " tries."
    nTries = 0
    Form_Load
    ElseIf Text1.Text > nAnswer then
    MsgBox "Lower"
    ElseIf Text1.Text < nAnswer then
    MsgBox "Higher"
    End If
    End If
    else
    MsgBox "Not a number!"
    End If
    End Sub

    private Sub Form_Load()
    Randomize
    nAnswer = (Rnd * 100) + 1
    End Sub



    -K


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

Featured