循环中的“预期声明结束”

时间:2014-02-21 14:55:00

标签: vbscript

我在第26行收到“预期声明结束”错误,第三行到最后一行。如果你看代码,它是一个简单的游戏,有一个人输入一个单词,脚本用下划线替换所有辅音,第二个玩家必须猜测这个单词。我认为,第26行是该程序唯一的错误。

phrase=inputbox("Player 1: Enter a phrase","Guessing Game")
phrase=answer
phrase=Replace(phrase,"A","_")
phrase=Replace(phrase,"B","_")
phrase=Replace(phrase,"C","_")
phrase=Replace(phrase,"D","_")
phrase=Replace(phrase,"F","_")
phrase=Replace(phrase,"G","_")
phrase=Replace(phrase,"H","_")
phrase=Replace(phrase,"J","_")
phrase=Replace(phrase,"K","_")
phrase=Replace(phrase,"L","_")
phrase=Replace(phrase,"M","_")
phrase=Replace(phrase,"N","_")
phrase=Replace(phrase,"P","_")
phrase=Replace(phrase,"Q","_")
phrase=Replace(phrase,"R","_")
phrase=Replace(phrase,"S","_")
phrase=Replace(phrase,"T","_")
phrase=Replace(phrase,"V","_")
phrase=Replace(phrase,"W","_")
phrase=Replace(phrase,"X","_")
phrase=Replace(phrase,"Y","_")
phrase=Replace(phrase,"Z","_")
Do
result=InputBox "Player 2 enter your guess for" & phrase , "Guessing Game"
Loop until result==answer
msgbox "You got it!",vbokonly,"Guessing Game"

4 个答案:

答案 0 :(得分:2)

在VBScript中,'equal'比较运算符是=。所以改变

Loop until result==answer

==>

Loop until result = answer

答案 1 :(得分:1)

您收到了该错误,因为您在分配中使用了一个函数,而没有将其参数列表放在括号中。改变这一行:

result=InputBox "Player 2 enter your guess for" & phrase , "Guessing Game"

进入这个:

result=InputBox("Player 2 enter your guess for" & phrase , "Guessing Game")

这是VBScript的问题之一:根据您调用过程或函数的位置/方式,您必须或不能将参数列表放在括号中。

>>> String 3, "*"         'must not use parentheses here
>>> String(3, "*")
Cannot use parentheses when calling a Sub (0x414)

>>> Call String(3, "*")   'must use parentheses here
>>> Call String 3, "*"
Expected end of statement (0x401)

>>> v = String(3, "*")    'must use parentheses here either
>>> v = String 3, "*"
Expected end of statement (0x401)

更糟糕的是,有些情况下无论如何都可以使用括号,因为它们在这种情况下有不同的含义:

>>> Hour(Now)

这实际上有效,因为这里的括号表示“参数列表”,而是“按值传递此参数”。看看this article关于括号可以在VBScript中创建的许多有趣情况。

正如Ekkehard.Horner已经指出的那样,您的脚本中的另一个错误是您使用==代替=来比较值。

作为旁注:您似乎假设输入将始终仅由大写字母组成,但您从不在任何地方强制执行。您可能希望UCase输入或添加检查以验证输入。

答案 2 :(得分:0)

    Dim phrase As String = InputBox("Player 1: Enter a phrase", "Guessing Game")
    Dim charCount As Integer = phrase.Length
    Dim strMask As String = vbEmpty

    For x = 1 To charCount
        strMask &= "_"
    Next

    Dim guess As String = vbEmpty
    Do
        guess = InputBox("Player 2 enter your guess for " & strMask, "Guessing Game")
    Loop Until phrase = guess

    MsgBox("You got it!", vbOKOnly, "Guessing Game")

答案 3 :(得分:0)

看到jmvp在优化你的程序方面做得很好,这让我想做同样的事情! :)

这是我的版本。我添加了一个常量来指定应用程序名称(因为它已经使用了几次),并且还允许玩家2通过单击取消(或不输入任何内容)退出播放。

Const APP_TITLE = "Guessing Game"
strPhrase = InputBox("Player 1: Enter a phrase", APP_TITLE)

With New RegExp
    .Pattern = "[^AEIOU]"
    .Global = True
    strDisplay = .Replace(strPhrase, "_")
End With

Do
    strResult = InputBox("Player 2: Enter your guess for " & strDisplay, APP_TITLE)
Loop Until StrComp(strPhrase, strResult, vbBinaryCompare) = 0 Or Len(strResult) = 0

If Len(strResult) > 0 Then MsgBox "You got it!", vbOkOnly, APP_TITLE