用于石头、布、剪刀的VBScript

用于石头、布、剪刀的VBScript,vbscript,Vbscript,首先让我先说声谢谢你的帮助 我正在使用VBScript为学校编写脚本。我过去从来没有使用过这个脚本,但它看起来并不太可怕。我的任务是在游戏结果中添加一个结果显示。以下是我到目前为止的情况。如果我使用所有“如果”语句,脚本将运行,但它将显示所有游戏结果。将“IF”语句更改为and“ELSEIF”语句将返回以结果脚本第二行中的“THEN”语句开头的语法错误。有没有人能给我指出正确的方向,我错过了什么,或者我做得不对。再次感谢您的时间和考虑 'Formally declare variables us

首先让我先说声谢谢你的帮助

我正在使用VBScript为学校编写脚本。我过去从来没有使用过这个脚本,但它看起来并不太可怕。我的任务是在游戏结果中添加一个结果显示。以下是我到目前为止的情况。如果我使用所有“如果”语句,脚本将运行,但它将显示所有游戏结果。将“IF”语句更改为and“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
”在尝试使用脚本之前,正式声明脚本使用的变量
调暗WshShl,回答,CardImage
'创建WScript对象的实例,以便以后使用
'弹出式方法
设置WshShl=WScript.CreateObject(“WScript.Shell”)
'输入球员姓名
Reply1=InputBox(“你好,你叫什么名字?”)
'显示问候语和播放器名称
MsgBox“您好”&回复1&“!欢迎来到石头,剪纸!”
“展示游戏规则
弹出“欢迎来到石头、布和剪刀游戏。这里是”&_
游戏规则:1.猜和电脑一样的东西_
“打成平局。2.报纸报道摇滚乐并获胜。3.摇滚乐突破”_
“剪刀和胜利。4.剪刀剪纸和胜利。”
'提示用户选择一个选项
答案=输入框(“键入纸、石头或剪刀。”_
“让我们玩个游戏吧!”
“是计算机随机选择的时候了
随机化
GetRandomNumber=Round(FormatNumber(Int((3*Rnd())+1)))
'为随机选择的数字指定一个值
如果GetRandomNumber=3,则CardImage=“rock”
如果GetRandomNumber=2,则CardImage=“剪刀”
如果GetRandomNumber=1,则CardImage=“纸张”
'显示游戏结果,以便用户可以看到他或她是否赢了
WshShl.弹出“您选择:”&答案和空格(12)和“计算机选择:”&_
心脏成像
如果Answer=“Rock”和Computer“Paper”,则MsgBox“Paper cover Rock:Computer Wins”
ElseIfAnswer=“纸”和计算机“剪刀”,然后MsgBox“剪刀剪纸:计算机赢”
ElseIfAnswer=“剪刀”和计算机“石头”,然后MsgBox“石头破剪刀:计算机赢”
ElseIfComputer=“Rock”&回答“Paper”,然后回答MsgBox“Paper覆盖Rock:你赢了”
ElseIfComputer=“Paper”&回答“剪刀”,然后回答MsgBox“剪刀剪布:你赢了”
ElseIfComputer=“剪刀”&回答“石头”,然后回答MsgBox“石头打断剪刀:你赢了”
ElseIfComputer=“Rock”&回答“Rock”,然后回答MsgBox“TIE”
ElseIfComputer=“Paper”&回答“Paper”,然后回答MsgBox“TIE”
Else Computer=“Scissor”并回答“Scissor”,然后回答MsgBox“TIE”
如果结束

您需要在
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 "
ElseIf Answer=“纸”和计算机“剪刀”,然后MsgBox“剪刀剪纸:计算机赢”
ElseIf Answer=“剪刀”和计算机“石头”,然后MsgBox“石头破剪刀:计算机赢”
ElseIf Computer=“Rock”&回答“Paper”,然后MsgBox“Paper cover Rock:你赢了”
ElseIf Computer=“Paper”&回答“剪刀”,然后回答MsgBox“剪刀剪纸:你赢了”
ElseIf Computer=“剪刀”&回答“石头”,然后MsgBox“石头砸剪刀:你赢了”
ElseIf Computer=“Rock”&回答“Rock”,然后回答MsgBox“TIE”
ElseIf Computer=“Paper”并回答“Paper”,然后回答MsgBox“TIE”

关键字和变量之间需要空格<如果是两个单词,则为code>Else。逻辑运算符是
,而不是
。您的上一个条件没有意义,因为没有
If
Else
不允许随后出现条件(整个想法是,如果前面的条件都不匹配,那么Else语句将运行)。我认为您可能应该花一点时间来复习基本语法。

看看这个例子:

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 & "!"

我最初在“ElseIf”语句后有一个空格,它在该行的第一个字符处返回语法错误。
Randomize
不应在循环中使用。@Ekkehard.Horner我已修复它。