Results 1 to 2 of 2

Thread: Issue with Visual Basic Code

  1. #1
    Join Date
    Mar 2009
    Posts
    89

    Issue with Visual Basic Code

    Hello friends,

    I have written one code where you select either ball,bat or stump instead of that it gives you only one winner (Computer wins),but in my code it just displays all the Message boxes.I don't know where i have made mistake.

    Code:
    'Formally declare variables used by the script before trying to use them
    Dim WshShl, Answer, CardImage
    
    'Create an instance of the WScript object in order to later use the
    'Popup method
    Set WshShl = WScript.CreateObject("WScript.Shell")
    
    'Display the rules of the game
    WshShl.Popup "Welcome to ball, bat and stump game. Here are the " & _
      "rules of the game: 1. Guess the same thing as the computer " & _
      "to tie. 2. Bat covers Ball and wins. 3. Ball breaks " & _ 
      "stump and wins. 4. Stump hit bat and wins."
    
    'Prompt the user to select a choice
    Answer = InputBox("Type  Ball, Bat and Stump.", _
      "Let's play a game!")
    
    'Time for the computer to randomly pick a choice
    Randomize
    GetRandomNumber = Round(FormatNumber(Int((3 * Rnd()) + 1)))
    
    'Assign a value to the randomly selected number
    If GetRandomNumber = 3 then CardImage = "Ball"
    If GetRandomNumber = 2 then CardImage = "Bat"
    If GetRandomNumber = 1 then CardImage = "Stump"
    
    'Display the game's results so that the user can see if he or she won
    WshShl.Popup "You picked: " & Answer & Space(12) & "Computer picked: " & _
      CardImage
    
    If Answer = "ball" & CardImage <> "bat" then _
    MsgBox "Bat Covers Ball:Computer wins!"
    
    If Answer = "bat" & CardImage <> "Stump" then MsgBox "Stump Cuts bat:Computer wins!"
    
    If Answer = "stump" & CardImage <> "ball" then MsgBox "Ball Breaks Stump:Computer wins!"
    
    If CardImage = "ball" & Answer <> "bat" then MsgBox "Bat Covers ball:You wins!"
    
    If CardImage = "bat" & Answer<> "Stump" then MsgBox "Stump Cuts Bat:You wins!"
    
    If CardImage = "stump" & Answer <> "ball" then MsgBox "Ball Breaks Stump:You wins!"
    
    If CardImage = "bat" & Answer <> "bat" then MsgBox "TIE!"
    
    If CardImage = "stump" & Answer<> "stump" then MsgBox "TIE!"
    
    If CardImage = "ball" & Answer <> "ball" then MsgBox "TIE!"

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Re: Issue with Visual Basic Code

    Sure i will help you out with this as i can you you have made a very small mistake while using Operator

    In your Code you have written as

    Code:
    If Answer = "ball" & CardImage <> "bat" then _
    but it has to be as follows


    Code:
    If Answer = "ball" AND  CardImage <> "bat" then _
    try to replace it every where you have put & as AND.

Similar Threads

  1. Protect Visual Basic for Applications code
    By Jalela in forum Windows Software
    Replies: 6
    Last Post: 10-07-2011, 11:13 AM
  2. Replies: 2
    Last Post: 12-03-2010, 06:14 AM
  3. Save button code in visual basic 2008
    By Chrisch in forum Software Development
    Replies: 4
    Last Post: 23-08-2009, 01:20 AM
  4. Error Code 1330 while installing Visual Basic 2008
    By Capers in forum Windows Software
    Replies: 2
    Last Post: 02-05-2009, 07:11 PM
  5. Issue with connection database in Visual Basic
    By Capers in forum Software Development
    Replies: 3
    Last Post: 08-04-2009, 08:40 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,936,362.52241 seconds with 16 queries