我在这个脚本中做错了什么?
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
如果你能弄清楚出了什么问题,那就太好了。
它一直在说
预期的"结束"
当我按照它所说的去做时,我会运行它,但它仍然不起作用。
已编辑代码以正确缩进它,这应该有助于突出显示问题。错误是因为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
语句只能返回 True
或 False
。
但是,也有一些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 时无法使用括号
但仍允许在没有返回值时用括号括起来的函数。
您可能还注意到,我用命名常量替换了
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()
来处理第一次初始猜测和随后的"重试"尝试。这意味着我们不会再次复制同一段代码,请参阅 DRY 原则。InputBox()
返回一个字符串,所以起初代码试图将字符串值与整数值进行比较,这将给出各种奇怪的结果。通过使用CInt()
强制转换为整数,比较开始按预期工作。