首页 文章

错误预期错误“结束”

提问于
浏览
1

我在这个剧本中做错了什么?

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

如果你能弄清楚出了什么问题会很棒 .

一直在说

预期“结束”

当我做它说的话我将运行它仍然无法正常工作 .

1 回答

  • 2

    已编辑代码以正确缩进它,这有助于突出显示该问题 . 该错误是因为 If 语句只能有一个 Else 条件, If 语句的基本结构是;

    If <boolean condition> Then
      <true outcome>
    Else
      <false outcome>
    End If
    

    目前你有多个实例

    If <boolean condition> Then
      <true outcome>
    Else
      <false outcome>
    Else
      <unknown condition>
    End If
    

    这是无效的语法,因为基本的 If 语句只能返回 TrueFalse .

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

    看起来像这样;

    If <boolean condition> Then
      <true outcome>
    ElseIf <boolean condition> Then
      <true outcome>
    ElseIf <boolean condition> Then
      <true outcome>
    Else
      <false outcome>
    End If
    

    根据您如何嵌套 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
    

    关于上面的例子,要注意几点

    • 在行中用 = 替换了 +
    guess+inputbox("guess the number","guess","guess here")
    

    因为它不会将 InputBox() 的结果分配给 guess ,这是我假设你正在做的事情 . 如果你试图将 InputBox() 的结果连接到 guess 仍然不起作用你将不得不使用

    guess = guess + InputBox("guess the number", "guess", "guess here")
    

    如果是这种情况虽然我个人更喜欢使用 & 而不是 + 来进行字符串连接 .

    • MsgBox() 中的括号似乎有点奇怪,如果没有将结果返回给变量,通常你会调用 MsgBox() ,如下所示;
    MsgBox "correct", vbOKOnly + vbInformation + vbSystemModal, "guess"
    

    但如果你确实想要包括括号(我喜欢这样做),你可以像这样使用 Call

    Call MsgBox("correct", vbOKOnly + vbInformation + vbSystemModal, "guess")
    

    这避免了可怕的

    Microsoft VBScript编译错误:调用Sub时无法使用括号

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

    • 您可能还注意到我用Named Constants替换了 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
    

    从中得到的东西;

    • 我们只需要循环内的一个 InputBox() 来处理第一个初始猜测和随后的"try again"尝试 . 这意味着我们不会再次复制相同的代码,请参阅DRY Principle .

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

相关问题