2015-07-12 63 views
0

基本上我試圖做一個.vbs文件,給你十個隨機加法問題。然後,在輸入框中輸入給定問題的答案。除了If/Else陳述之外,一切都可以正常工作,告訴你自己是對還是錯。如果它說「什麼是2 + 2」,而我輸入「4」(不含引號),那麼它會輸出「(你攻擊)TRIP!POW!哎呦錯誤如果你鍵入:下面的代碼:檢查是否(數字)加上(數字)等於你輸入的答案

msgbox("Starting addition. Press OK to begin.") 
dim i 
i = 0 

Do 
    i = i + 1 
    'i is for the question timer 
    Dim max,min 
    max=100 'max random 
    min=1 'min random 
    dim j, k, l 'part part total 

    Randomize 
    msgbox("What is") 
    j = Int((max-min+1)*Rnd+min) 
    msgbox(j) 
    msgbox("plus") 
    k = Int((max-min+1)*Rnd+min) 
    msgbox(k) 
    answer = Inputbox("I hope you got all of that... ^_^") 
    l = j + k 

    if answer = l then 
    msgbox("(You attack) BAM! Right on target") 
    else 
    msgbox("(You attack) TRIP! POW! ouchy wrong") 
    msgbox("You could've gotten it right IF you typed:") 
    msgbox(l) 
    end if 
loop until i = 10 
+0

歡迎來到[so]。請添加更多令人滿意的代碼[如何創建最小,完整和可驗證的示例](http://stackoverflow.com/help/mcve) – JosefZ

+0

好吧,我編輯它以顯示我的整個程序。謝謝! – oBSERVEr

回答

1

閱讀在Comparison Operators (VBScript)參考:

...表情如何比較或從什麼比較, 取決於底層亞型結果:
...
如果一個表達式是數字,另一個是字符串那麼數字表達式較少字符串表達式。

j = Int((max-min+1)*Rnd+min) 
k = Int((max-min+1)*Rnd+min) 
answer = Inputbox("I hope you got all of that... ^_^" _ 
    & vbCR & "What is " & j & " plus " & k) 
l = j + k 
If IsNumeric(answer) then 
    answer = Int(answer) 
Else 
    answer = l - 1 
End If 
if answer = l then 
    msgbox("(You attack) BAM! Right on target") 
else 
    msgbox("(You attack) TRIP! POW! ouchy wrong") 
    msgbox("You could've gotten it right IF you typed:") 
    msgbox(l) 
end if 
相關問題