也许我没有看到它,但我真的相信我有永远的循环!有人可以帮忙吗?我从一开始就开始这样做,我可以让用户再次使用代码。谢谢你的帮助。
代码:
'Written by Douglas Howe
Option Explicit
Dim colorPicOne, colorPicTwo, tryAgain
'initial do
do
'first prompt
do
Wscript.StdOut.WriteLine "Pick a primary color (i.e blue, red, or yellow): "
colorPicOne = lcase(wscript.StdIn.ReadLine)
if colorPicOne <> "blue" and colorPicOne <> "red" and colorPicOne <> "yellow" then
Wscript.StdOut.WriteLine "Not a primary color! Please pick another color: "
else
exit do
end if
loop while colorPicOne <> "blue" OR colorPicOne <> "red" or colorPicOne <> "yellow"
'second prompt
do
Wscript.StdOut.WriteLine "Pick another primary color (i.e blue, red, or yellow): "
colorPicTwo = lcase(wscript.StdIn.ReadLine)
if colorPicTwo <> "blue" and colorPicTwo <> "red" and colorPicTwo <> "yellow" then
Wscript.StdOut.WriteLine "Not a primary color! Please pick another color: "
else
exit do
end if
loop while colorPicTwo <> "blue" OR colorPicTwo <> "red" or colorPicTwo <> "yellow"
if colorPicOne = "red" and colorPicTwo = "blue" then
Wscript.StdOut.Writeline "Your color is purple!"
else
if colorPicOne = "blue" and colorPicTwo ="red" then
Wscript.StdOut.Writeline "Your color is purple!"
end if
if colorPicOne = "red" and colorPicTwo = "yellow" then
Wscript.StdOut.Writeline "Your color is orange!"
else
if colorPicOne = "yellow" and colorPicTwo ="red" then
Wscript.StdOut.Writeline "Your color is orange!"
end if
if colorPicOne = "blue" and colorPicTwo = "yellow" then
Wscript.StdOut.Writeline "Your color is green!"
else
if colorPicOne = "yellow" and colorPicTwo ="blue" then
Wscript.StdOut.Writeline "Your color is green!"
end if
Wscript.Stdout.WriteLine "Would you like to try again? (yes or no) :"
tryAgain =lcase(Wscript.StdIn.ReadLine)
if tryAgain = "no" then
Wscript.Stdout.WriteLine "Have a nice day"
exit do
if tryAgain = "yes" then
Wscript.Stdout.WriteLine "Ok!"
else
Wscript.Stdout.WriteLine "Sorry, you gave an invalid response. Good Bye!"
exit do
end if
end if
loop while tryAgain = "yes"
我的错误表明我的loop
没有do
。
答案 0 :(得分:1)
问题不在于你有一个循环而没有那么多,你有几个(我发现3个)块if
语句没有用end if
正确关闭。在最终的嵌套级别中,您的最终loop
没有do
。我认为这就是你的意图:
'Written by Douglas Howe
Option Explicit
Dim colorPicOne, colorPicTwo, tryAgain
'initial do
do
'first prompt
do
Wscript.StdOut.WriteLine "Pick a primary color (i.e blue, red, or yellow): "
colorPicOne = lcase(wscript.StdIn.ReadLine)
if colorPicOne <> "blue" and colorPicOne <> "red" and colorPicOne <> "yellow" then
Wscript.StdOut.WriteLine "Not a primary color! Please pick another color: "
else
exit do
end if
loop while colorPicOne <> "blue" OR colorPicOne <> "red" or colorPicOne <> "yellow"
'second prompt
do
Wscript.StdOut.WriteLine "Pick another primary color (i.e blue, red, or yellow): "
colorPicTwo = lcase(wscript.StdIn.ReadLine)
if colorPicTwo <> "blue" and colorPicTwo <> "red" and colorPicTwo <> "yellow" then
Wscript.StdOut.WriteLine "Not a primary color! Please pick another color: "
else
exit do
end if
loop while colorPicTwo <> "blue" OR colorPicTwo <> "red" or colorPicTwo <> "yellow"
if colorPicOne = "red" and colorPicTwo = "blue" then
Wscript.StdOut.Writeline "Your color is purple!"
else
if colorPicOne = "blue" and colorPicTwo ="red" then
Wscript.StdOut.Writeline "Your color is purple!"
end if 'This was missing
end if
if colorPicOne = "red" and colorPicTwo = "yellow" then
Wscript.StdOut.Writeline "Your color is orange!"
else
if colorPicOne = "yellow" and colorPicTwo ="red" then
Wscript.StdOut.Writeline "Your color is orange!"
end if
if colorPicOne = "blue" and colorPicTwo = "yellow" then
Wscript.StdOut.Writeline "Your color is green!"
else
if colorPicOne = "yellow" and colorPicTwo ="blue" then
Wscript.StdOut.Writeline "Your color is green!"
end if 'This was missing
end if
Wscript.Stdout.WriteLine "Would you like to try again? (yes or no) :"
tryAgain =lcase(Wscript.StdIn.ReadLine)
if tryAgain = "no" then
Wscript.Stdout.WriteLine "Have a nice day"
exit do
if tryAgain = "yes" then
Wscript.Stdout.WriteLine "Ok!"
else
Wscript.Stdout.WriteLine "Sorry, you gave an invalid response. Good Bye!"
exit do
end if 'This was missing
end if
end if
loop while tryAgain = "yes"
答案 1 :(得分:0)
错误消息具有误导性。问题不在于您Loop
没有Do
,而是Loop
位于同一地方没有Do
的地方上下文。
这是您观察到的行为的实际原因:
if colorPicOne = "red" and colorPicTwo = "blue" then
Wscript.StdOut.Writeline "Your color is purple!"
else
if colorPicOne = "blue" and colorPicTwo ="red" then
Wscript.StdOut.Writeline "Your color is purple!"
end if
从缩进的角度来看,你可能意味着:
if colorPicOne = "red" and colorPicTwo = "blue" then
Wscript.StdOut.Writeline "Your color is purple!"
else
if colorPicOne = "blue" and colorPicTwo ="red" then Wscript.StdOut.Writeline "Your color is purple!"
end if
但你实际拥有的(有适当的缩进)是这样的:
if colorPicOne = "red" and colorPicTwo = "blue" then
Wscript.StdOut.Writeline "Your color is purple!"
else
if colorPicOne = "blue" and colorPicTwo ="red" then
Wscript.StdOut.Writeline "Your color is purple!"
end if
end if '<-- missing "end if" here!
基本上,您在另一个If..End If
表达式的Else
分支中嵌套了If
,而没有关闭Else
分支。因为所有后续语句也嵌套在该分支内,所以第一个Do
和最后一个Loop
处于不同的上下文中。
在VBScript中,您可以像这样编写If
表达式:
If condition Then statement Else other_statement
或者像这样:
If condition Then
statement
Else
other_statement
End If
即。如果您将statement
放在新行上,则必须使用End If
关闭整个表达式。此规则的唯一例外是使用行继续运算符(_
)包装行:
If condition Then _
statement
上述内容在没有End If
的情况下有效,因为它在技术上只是一行。