2016-08-07 2 views
1

Позвольте мне вначале начать с того, чтобы заранее поблагодарить вас за любую помощь.VBScript для исхода камней, бумаги, ножниц

Я работаю сценарий для школы, используя 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 

ответ

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 " 
+0

я первоначально имел место после заявления «ELSEIF» возвращает ошибку синтаксиса на первый символ для этой строки. – BreezyHamilton

0

Вам нужны пробелы между ключевыми словами и переменными. Else If - два слова. Логическим оператором является And, а не &. Ваше последнее условие не имеет смысла, потому что Else без If не дает условия после этого (вся идея является выражением Else, если ни одно из предыдущих условий не соответствует). Я думаю, вы, вероятно, потратили немного времени на анализ базового синтаксиса.

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

'' Randomize'' не следует использовать в цикле. –

+0

@ Ekkehard.Horner Я исправил его. – omegastripes

Смежные вопросы