VBScript Number Guessing Game

心已入冬 提交于 2019-12-02 15:01:33

问题


I am at a complete loss here. I have been beating my head against a wall all weekend and I cannot for the life of me figure out how to make my program work. This is a college class assignment and it is my first programming assignment ever, so this is a real challenge for me. I have Googled until my fingers were bleeding (not really) and I have only come across marginally helpful information. Here is my assignment (I am deeply sorry if I get the formatting wrong):

'Initialization Section
Option Explicit
Const cGreetingMsg = "Pick a number between 1 - 100"
Dim intUserNumber, intRandomNo, strOkToEnd, intNoGuesses
intNoGuesses = 0

'Main Processing Section
'Generate a random number
Randomize
intRandomNo = FormatNumber(Int((100 * Rnd) + 1))

'Loop until either the user guesses correctly or the user clicks on  Cancel
Do Until strOkToEnd = "yes"

  'Prompt user to pick a number
  intUserNumber = InputBox("Type your guess:",cGreetingMsg)
  intNoGuesses = intNoGuesses + 1

  'See if the user provided an answer
  If Len(intUserNumber) <> 0 Then

'Make sure that the player typed a number
If IsNumeric(intUserNumber) = True Then

  'Test to see if the user's guess was correct
  If FormatNumber(intUserNumber) = intRandomNo Then
    MsgBox "Congratulations! You guessed it. The number was " & _
      intUserNumber & "." & vbCrLf & vbCrLf & "You guessed it " & _
      "in " & intNoGuesses & " guesses.", ,cGreetingMsg
    strOkToEnd = "yes"
  End If

  'Test to see if the user's guess was too low
  'Assignment 3: Add another If/ElseIf/Else Condition to check for if the user is less than 
  '80,60,40,20,10 or farther from the correct guess, but still too low        
  If FormatNumber(intUserNumber) < intRandomNo Then 

  strOkToEnd = "no"
  End If

  'Test to see if the user's guess was too high
  'Assignment 3: Add another If/ElseIf/Else Condition to check for if the user is more than 
  '80,60,40,20,10 or closer to the correct guess, but still too high  
  If FormatNumber(intUserNumber) > intRandomNo Then

  strOkToEnd = "no"
  End If      

Else
  MsgBox "Sorry. You did not enter a number. Try again.", , cGreetingMsg
End If

  Else
    MsgBox "You either failed to type a value or you clicked on Cancel. " & _
      "Please play again soon!", , cGreetingMsg
    strOkToEnd = "yes"
  End If

Loop

This is blowing my mind. The only thing that has been remotely helpful has been this link: VBScript Guess a number and with that being said, I have no earthly idea how to even implement that code into my own. It looks like it would work, but whenever I try it VBScript throws tons of "expected end of statement" errors. My own code is near worthless in the effort, but here it is:

'Initialization Section

Option Explicit

Const cGreetingMsg = "Pick a number between 1 - 100"

Dim intUserNumber, intRandomNo, strOkToEnd, intNoGuesses

intNoGuesses = 0

'Main Processing Section

'Generate a random number
Randomize
intRandomNo = FormatNumber(Int((100 * Rnd) + 1))

'Loop until either the user guesses correctly or the user clicks on Cancel
Do Until strOkToEnd = "yes"

'Prompt user to pick a number
intUserNumber = InputBox("Type your guess:",cGreetingMsg)
intNoGuesses = intNoGuesses + 1

'See if the user provided an answer
If Len(intUserNumber) <> 0 Then
'Make sure that the player typed a number
If IsNumeric(intUserNumber) = True Then

'Test to see if the user's guess was correct
If FormatNumber(intUserNumber) = intRandomNo Then
MsgBox "Congratulations! You guessed it. The number was " & _
intUserNumber & "." & vbCrLf & vbCrLf & "You guessed it " & _
 "in " & intNoGuesses & " guesses.", ,cGreetingMsg
strOkToEnd = "yes"
End If

'Test to see if the user's guess was too low
If FormatNumber(intUserNumber) < intRandomNo - 80 Then
    MsgBox "Your guess was too low by 80. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) < intRandomNo - 60 Then
    MsgBox "Your guess was too low by 60. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) < intRandomNo - 40 Then
    MsgBox "Your guess was too low by 40. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) < intRandomNo - 20 Then
    MsgBox "Your guess was too low by 20. Try again", ,cGreetingMsg
Elseif FormatNumber(intUserNumber) < intRandomNo - 10 Then
    MsgBox "Your guess was too low by 10. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) > intRandomNo Then
    MsgBox "Your guess was still too low. Try again, please", ,cGreetingMsg
    strOkToEnd = "no"
End If

'Test to see if the user's guess was too high
If FormatNumber(intUserNumber) > intRandomNo - 80 Then
    MsgBox "Your guess was too high by 80. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) > intRandomNo + 60 Then
    MsgBox "Your guess was too high by 60. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) > intRandomNo + 40 Then
    MsgBox "Your guess was too high by 40. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) > intRandomNo + 20 Then
    MsgBox "Your guess was too high by 20. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) > intRandomNo + 10 Then
    MsgBox "Your guess was too high by 10. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) < intRandomNo Then
    MsgBox "Your guess was still too low. Try again, please", ,cGreetingMsg
    strOkToEnd = "no"
End If

Else
    MsgBox "Sorry. You did not enter a number. Try again.", , cGreetingMsg
End If

Else
    MsgBox "You either failed to type a value or you clicked on Cancel. " & _
    "Please play again soon!", , cGreetingMsg
    strOkToEnd = "yes"
End If

Loop

Any and all help is greatly appreciated with this. Thank you.


回答1:


Your main problem here is that all your numbers are really strings. The FormatNumber function takes a number and returns a string so intRandomNo is a string. InputBox also returns a string meaning intUserNumber is also a string. It will not be possible to calculate the difference if you keep those two values as strings. The first thing you have to do is to convert them to integers. I will not write all the code for you but I will give you a hint. First declare this variable:

Dim Difference

Then assign it this value inside your loop:

Difference = CInt(intRandomNo) - CInt(intUserNumber)

... and use that variable in your nested if. It should be easy after that.




回答2:


'Initialization Section
Option Explicit
Const cGreetingMsg = "Pick a number between 1 - 100"
Dim intUserNumber, intRandomNo, strOkToEnd, intNoGuesses, stringToNumber
intNoGuesses = 0

'Main Processing Section
'Generate a random number
Randomize
intRandomNo = FormatNumber(Int((100 * Rnd) + 1))

'Loop until either the user guesses correctly or the user clicks on Cancel
Do Until strOkToEnd = "yes"

  'Prompt user to pick a number
  intUserNumber = InputBox("Type your guess:",cGreetingMsg)
  intNoGuesses = intNoGuesses + 1

  'See if the user provided an answer
  If Len(intUserNumber) <> 0 Then

    'Make sure that the player typed a number
    If IsNumeric(intUserNumber) = True Then

      'Test to see if the user's guess was correct
      If FormatNumber(intUserNumber) = intRandomNo Then
        MsgBox "Congratulations! You guessed it. The number was " & _
          intUserNumber & "." & vbCrLf & vbCrLf & "You guessed it " & _
          "in " & intNoGuesses & " guesses.", ,cGreetingMsg
        strOkToEnd = "yes"
      End If

      'Test to see if the user's guess was too low
      'Assignment 3: Add another If/ElseIf/Else Condition to check for if the user is less than 
      '80,60,40,20,10 or farther from the correct guess, but still too low 

      If FormatNumber(intUserNumber) < intRandomNo Then
            MsgBox "The number you chose is too low", ,cGreetingMsg


       strOkToEnd = "no"
        End If

     If FormatNumber(intRandomNo) - intUserNumber = 10 Then
                MsgBox "Too low 10 off!", ,cGreetingMsg
    ElseIf FormatNumber(intRandomNo) - intUserNumber = 20 Then
                MsgBox "Too low 20 off!", ,cGreetingMsg
    ElseIf FormatNumber(intRandomNo) - intUserNumber = 40 Then
                MsgBox "Too low 40 off!", ,cGreetingMsg
    ElseIf FormatNumber(intRandomNo) - intUserNumber = 60 Then
                MsgBox "Too low 60 off!", ,cGreetingMsg
    ElseIf FormatNumber(intRandomNo) - intUserNumber = 80 Then
                MsgBox "Too low 80 off!", ,cGreetingMsg                                    

        strOkToEnd = "no"
         End If


      If FormatNumber(intUserNumber) > intRandomNo Then 
        MsgBox "The number you chose is too high!", ,cGreetingMsg

      strOkToEnd = "no"
      End If

      'Test to see if the user's guess was too high
      'Assignment 3: Add another If/ElseIf/Else Condition to check for if the user is more than 
      '80,60,40,20,10 or closer to the correct guess, but still too high  
       If FormatNumber(intUserNumber) - intRandomNo = 10 Then
                MsgBox "Too high 10 off!", ,cGreetingMsg
    ElseIf FormatNumber(intUserNumber) - intRandomNo = 20 Then
                MsgBox "Too high 20 off!", ,cGreetingMsg
    ElseIf FormatNumber(intUserNumber) - intRandomNo = 40 Then
                MsgBox "Too high 40 off!", ,cGreetingMsg
    ElseIf FormatNumber(intUserNumber) - intRandomNo = 60 Then
                MsgBox "Too high 60 off!", ,cGreetingMsg
    ElseIf FormatNumber(intUserNumber) - intRandomNo = 80 Then
                MsgBox "Too high 80 off!", ,cGreetingMsg                                    

        strOkToEnd = "no"
         End If    

    Else
      MsgBox "Sorry. You did not enter a number. Try again.", , cGreetingMsg
    End If

  Else
    MsgBox "You either failed to type a value or you clicked on Cancel. " & _
      "Please play again soon!", , cGreetingMsg
    strOkToEnd = "yes"
  End If

Loop


来源:https://stackoverflow.com/questions/42474949/vbscript-number-guessing-game

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!