2016-09-30 50 views
0

当我运行以下代码片段并输入可接受的值时,我会得到所需的结果。在输入框中验证用户输入

do while len(strselect) = 0 'or strselect<>"1" or strselect<>"2" or strselect<>"3" 
strselect = inputbox ("Please select:" &vbcrlf&vbcrlf&_ 
"1. Add an entry" &vbcrlf&vbcrlf&_ 
"2. Remove an entry" &vbcrlf&vbcrlf&_ 
"3. Search for an entry" &vbcrlf, "Contact Book") 
if isempty(strselect) then 
wscript.quit() 
elseif strselect="1" then 
wscript.echo "You chose 1" 
elseif strselect="2" then 
wscript.echo "You chose 2" 
elseif strselect="3" then 
wscript.echo "You chose 3" 
end if 
loop 

但是,如果我试图进一步限制验证过程(通过在do while条件的话),然后再次运行该代码段,我得到相应的if触发条件,但do循环继续,而不是退出, 。

我使用isnumericcstrdo循环strselect条件,没有快乐试过......我缺少的是拿到混账东西退出循环?

回答

0

你有逻辑问题在条件

  condition 1   condition 2  condition 3  condition 4 
     v----------------v  v------------v v------------v v............v 
do while len(strselect) = 0 or strselect<>"1" or strselect<>"2" or strselect<>"3" 

根据内部strselect价值,你有

value c1  c2  c3  c4  
     len=0 <>"1" <>"2" <>"3" c1 or c2 or c3 or c4 
-------------------------------------- -------------------- 
empty true true true true   true 
    1  false false true true   true 
    2  false true false true   true 
    3  false true true false   true 
other false true true true   true 

在你至少有一个条件评估为true每一行,因为您将条件与Or运算符连接在一起(如果至少有一个值为真,则计算结果为true),则完整条件评估为true并且代码保持循环运行

你只需要改变的条件

Do While strselect<>"1" And strselect<>"2" And strselect<>"3" 
Do While Not (strselect="1" Or strselect="2" Or strselect="3") 
.... 
+0

非常感谢MC ND,插图精美的答案。我的错误现在很清楚,因为昨晚它令人沮丧地无法确定。我会修改我的逻辑运算符和我的逻辑!后急...再次感谢! :) –