2016-08-07 3 views
1

まずは事前に何か助けていただきありがとうございます。VBScript for rock、paper、scissors outcome

私はVBScriptを使用して学校のスクリプトを作成しています。私は過去にこのスクリプトを使ったことは一度もありませんでしたが、使用するにはあまりにもひどいとは思われません。私が取り組んでいる課題は、ゲームの結果に結果表示を追加することです。以下は私がこれまで持っていたものです。すべての "IF"ステートメントを使用すると、スクリプトは実行されますが、すべてのゲーム結果が表示されます。 "IF"ステートメントを "ELSEIF"ステートメントに変更すると、結果スクリプトの2行目の "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は2ワードです。論理演算子はではなくAndです。 IfのないElseは、その後の条件を許可しないため、最後の条件は意味をなさない(前の条件のどれも一致しないと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

関連する問題