为什么这个 VBS 代码跳过第一个条件? [英] Why is this VBS code skipping the first condition?
问题描述
以下必须在 qtp 中工作,所以我不能使用 WScript.Echo.以下代码必须使用输入框要求输入 1 到 10 之间的整数.如果没有输入,它必须给出一条消息中止".
The following must work in qtp so i can not use WScript.Echo. The following code have to ask for an integer between 1 to 10 inclusive using an inputbox. If nothing entered the it has to give a message "Aborted".
如果输入了其他任何内容,则必须说明问题所在并再次询问该号码,直到我通过取消或不输入任何内容而中止.我有以下代码,但看起来它正在跳过第一个条件并转到循环中的第一个 else:
If anything else entered then it has to say what is the problem and ask again for the number until I abort by cancel or by entering nothing. I have the following code but it looks like it is skipping the first condition and goes to the first else in the loop:
Option Explicit
Dim vNum, sNum, nNum
Do
vNum = InputBox("Please enter an integer between 1 and 10 inclusive")
If IsEmpty(vNum) Then
msgbox("Aborted")
Exit Do
Else
sNum = Trim(vNum)
If "" = sNum Then
vNum=Inputbox("Empty string")
Else
If IsNumeric(sNum) Then
nNum = CDbl(sNum)
If nNum <> Fix(nNum) Then
vNum=inputbox("Not an Integer")
Else
If nNum < 1 Or nNum > 10 Then
vNum=inputbox ("Not in range")
Else
msgbox nNum,("number ok")
Exit Do
End If
End If
Else
vNum= inputbox ("Not a number")
End If
End If
End If
Loop
msgbox ("Done")
推荐答案
你可以每次循环修改指令信息:
You could loop and change the instruction message each time:
Dim vNum, instruction
instruction = "Please enter an integer between 1 and 10 inclusive"
Do
vNum = InputBox(instruction)
If vNum = False Then
MsgBox "Aborted"
Exit Do
ElseIf CStr(Trim(vNum)) = "" Then
instruction = "Empty string"
ElseIf Not IsNumeric(vNum) Then
instruction = "Not an integer"
ElseIf IsNumeric(vNum) And vNum < 1 Or vNum > 10 Then
instruction = "Not in range"
ElseIf IsNumeric(vNum) And vNum > 0 And vNum < 11 Then
MsgBox "Number OK"
Exit Do
Else
instruction = "Invalid Entry"
End If
Loop
这篇关于为什么这个 VBS 代码跳过第一个条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!