多条件循环 [英] Multiple condition loop

查看:28
本文介绍了多条件循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个 VBScript 循环,要求输入 1 到 10 之间的整数,包括如果输入了错误的符号或数字,则再次询问,直到从用户那里检索到所需的数字.

I need a VBScript loop asking to entered an integer between 1 to 10 including, if wrong symbol or number entered then asking again until the desired number is retrieved from user.

这是我试过的:

Option Explicit
Dim Num

Num=inputbox("Please enter integer number between 1 to 10")

'Checking if entered  value is numeric
Do while not isnumeric(Num) 
    Num=inputbox("Please enter integer number between 1 to 10", "INCORRECT SYMBOL")
Loop

Do while (Num<1 or Num>10)
    Num=inputbox("Please enter integer number between 1 to 10 ", "Number is NOT IN RANGE") 
Loop

Do while not int(Num)
     Num=inputbox("Please enter integer number between 1 to 10 ", "Number is NOT INTEGER") 
Loop

不起作用:例如,当我输入 3 时,我收到输入框说数字不是整数",当输入一个字母时,我收到错误消息类型不匹配字符串,错误代码 800A00D.

does not work: when I enter 3 for example I am getting inputbox saying "Number is NOT INTEGER", when entering a letter I receive error message Type mismatch string, error code 800A00D.

推荐答案

你需要一个 Loop.对于每个(变体)输入,您需要检查:

You need one Loop. For each (variant) input you need to check:

  1. 是否为空(用户按下取消或 X) - 中止
  2. 它是一个空白(零长度或空格序列)字符串
  3. 是数字吗
  4. 是整数吗
  5. 是否在范围内 - 接受

如:

Option Explicit

Dim vNum, sNum, nNum
Do
   vNum = InputBox("Please enter an integer beween 1 and 10 (inclusive)")
   If IsEmpty(vNum) Then
      WScript.Echo "Aborted"
      Exit Do
   Else
      sNum = Trim(vNum)
      If "" = sNum Then
         WScript.Echo "Empty string"
      Else
         If IsNumeric(sNum) Then
            nNum = CDbl(sNum)
            If nNum <> Fix(nNum) Then
               WScript.Echo "Not an Integer"
            Else
               If nNum < 1 Or nNum > 10 Then
                  WScript.Echo "Not in range"
               Else
                  WScript.Echo nNum, "is ok"
                  Exit Do
               End If
            End If
         Else
            WScript.Echo "Not a number"
         End If
      End If
   End If
Loop
WScript.Echo "Done"

对不同的数据类型使用不同的变量可能有点迂腐,但应该能说明为什么会出现类型问题.

Using different variables for the different data types may be pedantic, but should show why you had type problems.

你的

Do while not int(Num)

没有按预期工作,因为这里的 Num 是 1 到 10 之间的数字;四舍五入(不存在的)小数部分再次给出 Num ;在布尔上下文中计算的 Num/作为布尔值给出(总是)真.

does not work as expected because here Num is a number between 1 and 10; rounding off the (not existing) fractional part gives Num again; Num evaluated in a boolean context/as a bool gives (always) True.

更新评论:

修剪 删除空间从字符串的头部或尾部;WScript.Echo 将输出发送到控制台 (cscript) 或对话框 (wscript).

Trim removes space from the head or tail of a string; WScript.Echo sends output to the console (cscript) or a dialog box (wscript).

更新:

正如这个问题所示,我没有明确说明按取消或 X(关闭)将 vNum 设置为空变体,与空/零长度字符串不同.因此应将其视为用户打算中止的指示.

As this question shows, I didn't make clear that pressing Cancel or X (Close) sets vNum to an empty variant, which is different from an empty/zero length string. So it should be treated as indication of the users intention to abort.

顺便说一句:您需要阅读文档,但您不能总是相信它们(参见此处).

BTW: You need to read the Docs, but you can't believe them always (cf. here).

这篇关于多条件循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆