验证输入框中的用户输入

2023-12-09

当我运行以下代码片段并输入可接受的值时,我得到了所需的结果。

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循环继续,而不是退出。

我尝试过使用isnumeric and cstr on the do loop strselect条件,没有欢乐......我错过了什么让该死的东西退出循环?


你的条件逻辑有问题

         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),完整条件评估为 true 并且代码不断循环

你只需要改变条件

Do While strselect<>"1" And strselect<>"2" And strselect<>"3"
Do While Not (strselect="1" Or strselect="2" Or strselect="3")
....
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

验证输入框中的用户输入 的相关文章