Vbscript “预期错误的故障”;完";

Vbscript “预期错误的故障”;完";,vbscript,Vbscript,我在这个脚本中做错了什么 Randomize value=int((150 * Rnd + 1) * Rnd + lowerbound) guess+inputbox("guess the number","guess","guess here") if guess=value then msgbox("correct"),0+64+4096,("guess") wscript.quit else if guess < value then msgbox("too lo

我在这个脚本中做错了什么

Randomize
value=int((150 * Rnd + 1) * Rnd + lowerbound)
guess+inputbox("guess the number","guess","guess here")
if guess=value then
  msgbox("correct"),0+64+4096,("guess")
  wscript.quit
else
  if guess < value then
    msgbox("too low"),0+32+4096,("guess")
    once=1
  end if
else
  msgbox("too high"),0+32+4096,("guess")
end if
once=1
do
  guess=inputbox("try again","guess","guess here")
  if guess=value then
    msgbox("correct"),0+64+4096,("guess")
  else
    if guess < value then
      msgbox("too low"),0+32+4096,("guess")
    end if
  else
    msgbox("too high"),0+32+4096,("guess")
  end if
loop
随机化
值=整数((150*Rnd+1)*Rnd+lowerbound)
猜+输入框(“猜数字”、“猜”、“猜这里”)
如果猜测=值,则
msgbox(“正确”),0+64+4096(“猜测”)
wscript.quit
其他的
如果猜测<值,则
msgbox(“过低”),0+32+4096(“猜测”)
一次=1
如果结束
其他的
msgbox(“过高”),0+32+4096(“猜测”)
如果结束
一次=1
做
猜测=输入框(“重试”、“猜测”、“在此处猜测”)
如果猜测=值,则
msgbox(“正确”),0+64+4096(“猜测”)
其他的
如果猜测<值,则
msgbox(“过低”),0+32+4096(“猜测”)
如果结束
其他的
msgbox(“过高”),0+32+4096(“猜测”)
如果结束
环
如果你能找出哪里出了问题,那就太好了

它一直在说

预期“结束”


当我按照它所说的做时,我会运行它,但它仍然无法工作。

编辑了代码,使其正确缩进,这将有助于突出问题。错误是因为
If
语句只能有一个
Else
条件,
If
语句的基本结构是:

If-Then
其他的
如果结束
目前,您有多个

If-Then
其他的
其他的
如果结束
这是无效语法,因为基本
If
语句只能返回
True
False

但是也有
ElseIf
,它允许指定多个布尔条件并返回不同的结果

看起来像这样

Randomize
value = Int((150 * Rnd + 1) * Rnd + lowerbound)
guess = InputBox("guess the number", "guess", "guess here")

If guess = value Then
  Call MsgBox("correct", vbOKOnly + vbInformation + vbSystemModal, "guess")
  WScript.Quit
ElseIf guess < value Then
  Call MsgBox("too low", vbOKOnly + vbQuestion + vbSystemModal, "guess")
  once = 1
Else
  Call MsgBox("too high", vbOKOnly + vbQuestion + vbSystemModal, "guess")
End if
once = 1
Do
  guess = InputBox("try again", "guess", "guess here")
  If guess = value Then
    Call MsgBox("correct", vbOKOnly + vbInformation + vbSystemModal, "guess")
  ElseIf guess < value then
    Call MsgBox("too low", vbOKOnly + vbQuestion + vbSystemModal, "guess")
  Else
    Call MsgBox("too high", vbOKOnly + vbQuestion + vbSystemModal, "guess")
  End If
Loop
MsgBox "correct", vbOKOnly + vbInformation + vbSystemModal, "guess"
If-Then
那么埃尔塞夫呢
那么埃尔塞夫呢
其他的
如果结束
根据您嵌套
If
语句的方式,相关代码可以这样重新编写

Randomize
value = Int((150 * Rnd + 1) * Rnd + lowerbound)
guess = InputBox("guess the number", "guess", "guess here")

If guess = value Then
  Call MsgBox("correct", vbOKOnly + vbInformation + vbSystemModal, "guess")
  WScript.Quit
ElseIf guess < value Then
  Call MsgBox("too low", vbOKOnly + vbQuestion + vbSystemModal, "guess")
  once = 1
Else
  Call MsgBox("too high", vbOKOnly + vbQuestion + vbSystemModal, "guess")
End if
once = 1
Do
  guess = InputBox("try again", "guess", "guess here")
  If guess = value Then
    Call MsgBox("correct", vbOKOnly + vbInformation + vbSystemModal, "guess")
  ElseIf guess < value then
    Call MsgBox("too low", vbOKOnly + vbQuestion + vbSystemModal, "guess")
  Else
    Call MsgBox("too high", vbOKOnly + vbQuestion + vbSystemModal, "guess")
  End If
Loop
MsgBox "correct", vbOKOnly + vbInformation + vbSystemModal, "guess"
因为它不会将
InputBox()
的结果分配给
guess
,我假设您正在这样做。如果您试图将
InputBox()
的结果连接到
guess
,但仍然无法工作,则必须使用

guess = guess + InputBox("guess the number", "guess", "guess here")
如果是这样的话,我个人更喜欢使用
&
而不是
+
进行字符串连接

  • MsgBox()
    中的括号似乎有点奇怪,如果不将结果返回给变量,通常会调用不带括号的
    MsgBox()
    ,如下所示

    Randomize
    value = Int((150 * Rnd + 1) * Rnd + lowerbound)
    guess = InputBox("guess the number", "guess", "guess here")
    
    If guess = value Then
      Call MsgBox("correct", vbOKOnly + vbInformation + vbSystemModal, "guess")
      WScript.Quit
    ElseIf guess < value Then
      Call MsgBox("too low", vbOKOnly + vbQuestion + vbSystemModal, "guess")
      once = 1
    Else
      Call MsgBox("too high", vbOKOnly + vbQuestion + vbSystemModal, "guess")
    End if
    once = 1
    Do
      guess = InputBox("try again", "guess", "guess here")
      If guess = value Then
        Call MsgBox("correct", vbOKOnly + vbInformation + vbSystemModal, "guess")
      ElseIf guess < value then
        Call MsgBox("too low", vbOKOnly + vbQuestion + vbSystemModal, "guess")
      Else
        Call MsgBox("too high", vbOKOnly + vbQuestion + vbSystemModal, "guess")
      End If
    Loop
    
    MsgBox "correct", vbOKOnly + vbInformation + vbSystemModal, "guess"
    
    但是如果您确实想包括括号(我更喜欢这样做),您可以像这样使用
    Call

    Call MsgBox("correct", vbOKOnly + vbInformation + vbSystemModal, "guess")
    
    这就避免了恐惧

    Microsoft VBScript编译错误:调用子脚本时无法使用括号

    但仍然允许在没有返回值时用括号括起函数

  • 您可能还注意到,我将
    MsgBox()
    中的硬编码数值替换为其他人更容易阅读和解释其含义的数值。它们也被硬煮成VBScript,所以没有理由不使用它们


  • 考虑到你想做什么,我认为我可以重新考虑一下代码,希望它能像你期望的那样工作

    Dim value, guess, msg, once
    Randomize
    value = Int((150 * Rnd + 1) * Rnd + lowerbound)
    
    Do
      If once = 0 Then
        msg = "guess the number"
      Else
        msg = "try again"
      End If
      guess = CInt(InputBox(msg, "guess", "guess here"))
      If guess = value Then
        Call MsgBox("correct", vbOKOnly + vbInformation + vbSystemModal, "guess")
        WScript.Quit
      ElseIf guess < value then
        Call MsgBox("too low", vbOKOnly + vbQuestion + vbSystemModal, "guess")
        once = 1
      Else
        Call MsgBox("too high", vbOKOnly + vbQuestion + vbSystemModal, "guess")
        once = 1
      End If
    Loop
    
    Dim值,猜测,消息,一次
    随机化
    值=整数((150*Rnd+1)*Rnd+lowerbound)
    做
    如果once=0,则
    msg=“猜猜数字”
    其他的
    msg=“再试一次”
    如果结束
    guess=CInt(输入框(msg,“guess”,“guess here”))
    如果猜测=值,则
    调用MsgBox(“正确”,vbOKOnly+vbInformation+vbSystemModal,“猜测”)
    WScript.Quit
    ElseIf guess<则值
    调用MsgBox(“太低”,vbOKOnly+vbQuestion+vbSystemModal,“猜测”)
    一次=1
    其他的
    调用MsgBox(“过高”,vbOKOnly+vbQuestion+vbSystemModal,“猜测”)
    一次=1
    如果结束
    环
    
    从中汲取的东西

  • 我们只需要循环中的一个
    InputBox()
    ,就可以处理最初的猜测和随后的“重试”尝试。这意味着我们不再复制同一段代码,请参见

  • InputBox()
    返回一个字符串,因此起初代码试图将字符串值与整数值进行比较,这将给出各种奇怪的结果。通过使用
    CInt()
    强制转换为整数,比较开始按预期工作


  • 你的意思是经常使用
    else if
    而不是
    else
    。或者
    else
    下的行应该与
    else
    在同一行,以便将其转换为
    else if
    。此外,
    lowerbound
    未定义,因此将被视为0。或者,您可以用于更快、更可读的代码。@PankajJaju是
    选择大小写True
    ElseIf
    更快?这是值得商榷的…@Lankymart-根据他的说法,这个案子效率更高。根据我的经验,我验证了10k测试交易文件,并发现案例更快。