2016-09-30 55 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,插圖精美的答案。我的錯誤現在很清楚,因爲昨晚它令人沮喪地無法確定。我會修改我的邏輯運算符和我的邏輯!後急...再次感謝! :) –