首先让我先说谢谢你的任何帮助。
我正在使用VBScript为学校编写脚本。我以前从未使用过这个特殊的脚本,但使用它似乎并不太糟糕。我正在进行的任务是将结果显示添加到游戏结果中。以下是我到目前为止的情况。如果我使用所有“IF”语句,脚本将运行但它将显示所有游戏结果。将“IF”语句更改为“ELSEIF”语句将返回以结果脚本第二行中的“THEN”语句开头的语法错误。有人能指出我正确的方向,我错过了什么或我做错了什么。再次感谢您的时间和考虑。
'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")
'Input for player name
Reply1 = InputBox("Hello. What is your name?")
'Display greeting and player name
MsgBox "Hello " & Reply1 & "! Welcome to Rock, Paper Scissors!"
'Display the rules of the game
WshShl.Popup "Welcome to Rock, Paper and Scissors game. Here are the " & _
"rules of the game: 1. Guess the same thing as the computer " & _
"to tie. 2. Paper covers rock and wins. 3. Rock breaks " & _
"scissors and wins. 4. Scissors cut paper and wins."
'Prompt the user to select a choice
Answer = InputBox("Type Paper, Rock, or Scissors.", _
"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 = "rock"
If GetRandomNumber = 2 then CardImage = "scissor"
If GetRandomNumber = 1 then CardImage = "paper"
'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 = "Rock" & Computer <> "Paper" Then MsgBox "Paper Covers Rock: Computer Wins"
ElseIfAnswer = "Paper" & Computer <> "Scissors" Then MsgBox "Scissors Cuts Paper: Computer Wins"
ElseIfAnswer = "Scissors" & Computer <> "Rock" Then MsgBox "Rock Breaks Scissors: Computer Wins"
ElseIfComputer = "Rock" & Answer <> "Paper" Then MsgBox "Paper Covers Rock: You Win "
ElseIfComputer = "Paper" & Answer <> "Scissors" Then MsgBox "Scissors Cuts Paper: You Win "
ElseIfComputer = "Scissors" & Answer <> "Rock" Then MsgBox "Rock Breaks Scissors: You Win "
ElseIfComputer = "Rock" & Answer <> "Rock" Then MsgBox "TIE "
ElseIfComputer = "Paper" & Answer <> "Paper" Then MsgBox "TIE "
Else Computer = "Scissor" & Answer <> "Scissor" Then MsgBox "TIE "
End If
答案 0 :(得分:1)
您需要在ElseIf
之后添加一个空格。
ElseIf Answer = "Paper" & Computer <> "Scissors" Then MsgBox "Scissors Cuts Paper: Computer Wins"
ElseIf Answer = "Scissors" & Computer <> "Rock" Then MsgBox "Rock Breaks Scissors: Computer Wins"
ElseIf Computer = "Rock" & Answer <> "Paper" Then MsgBox "Paper Covers Rock: You Win "
ElseIf Computer = "Paper" & Answer <> "Scissors" Then MsgBox "Scissors Cuts Paper: You Win "
ElseIf Computer = "Scissors" & Answer <> "Rock" Then MsgBox "Rock Breaks Scissors: You Win "
ElseIf Computer = "Rock" & Answer <> "Rock" Then MsgBox "TIE "
ElseIf Computer = "Paper" & Answer <> "Paper" Then MsgBox "TIE "
答案 1 :(得分:0)
您的关键字和变量之间需要空格。 Else If
是两个字。逻辑运算符为And
,而不是&
。你的最后一个条件没有意义,因为没有Else
的{{1}}之后不允许一个条件(如果之前的条件都不匹配,则整个想法就是Else语句运行)。我想你应该花一点时间来回顾基本语法。
答案 2 :(得分:0)
看一下这个例子:
Option Explicit
Dim Reply, Answer, RandomNumber, Computer, Result, UserChoice
' Init the random number generator
Randomize
' Input for player name
Reply = InputBox("Hello. What is your name?")
' Display greeting and player name
MsgBox "Hello " & Reply & "! Welcome to Rock Paper Scissors!"
' Loop begin - start the turn
Do
' Display the rules of the game and prompt the user to select a choice
Do
Answer = InputBox(_
"Here are the rules of the game:" & vbCrLf & _
"1. Guess the same thing as the computer to tie." & vbCrLf & _
"2. Paper covers Rock and wins." & vbCrLf & _
"3. Rock breaks Scissors and wins." & vbCrLf & _
"4. Scissors cut Paper and wins." & vbCrLf & vbCrLf & _
"Type Paper, Rock, or Scissors.", "Let's play a game!")
Loop Until Answer = "Paper" Or Answer = "Rock" Or Answer = "Scissors" ' Repeat until proper user input
' Time for the computer to randomly pick a choice
RandomNumber = Int(3 * Rnd()) + 1
' Assign a value to the randomly selected number
Select Case RandomNumber
Case 1 Computer = "Paper"
Case 2 Computer = "Rock"
Case Else Computer = "Scissors"
End Select
' Check the game's results
If Answer = Computer Then
Result = "Tie"
Else
Select Case Answer & Computer
Case "PaperRock" Result = "Paper Covers Rock: You Win"
Case "RockScissors" Result = "Rock Breaks Scissors: You Win"
Case "ScissorsPaper" Result = "Scissors Cut Paper: You Win"
Case "RockPaper" Result = "Paper Covers Rock: Computer Win"
Case "ScissorsRock" Result = "Rock Breaks Scissors: Computer Win"
Case "PaperScissors" Result = "Scissors Cut Paper: Computer Win"
End Select
End If
' Display the game's results so that the user can see if he or she won
UserChoice = MsgBox(_
"You picked: " & Answer & vbCrLf & _
"Computer picked: " & Computer & vbCrLf & _
Result, vbRetryCancel, "Result")
Loop While UserChoice = vbRetry ' Check user choice to continue or exit
' Say goodbye
MsgBox "Bye " & Reply & "!"