我在此脚本中做错了什么?
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 Principle。
InputBox()
返回一个字符串,因此一开始代码尝试将字符串值与整数值进行比较,这将产生各种奇怪的结果。通过使用CInt()
比较将其转换为整数,按预期开始工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句