一个条件,多个事件

Lèl*_*ull 3 vbscript events conditional-statements

我试图弄清楚如何使用.vbs只使用一个条件来发生多个事件.(这里我尝试使用case语句.)是否有这样的命令,或者我是否必须为每一行重写命令?(这可以通过在之前的代码行中激活它来在记事本上输入.)

  msgbox("I was woundering how old you are.")
    age=inputbox("Type age here.",,"")
    Select Case age
    Case age>24
        x=age-24
        WshShell.SendKeys "I see you are "&x&" years older than me."
        WshShell.SendKeys "{ENTER}"
    Case age<24
        x=24-age
        WshShell.SendKeys "I see you are "&x&" years younger than me."
        WshShell.SendKeys "{ENTER}"
    Case age=24
        WshShell.SendKeys "Wow were the same age!"
        WshShell.SendKeys "{ENTER} "
    End Select
Run Code Online (Sandbox Code Playgroud)

Aut*_*aos 7

我认为你正在寻找Select Case True,增强了对Select Case交换机的使用:

age = inputbox("I was wondering how old you are.")

Select Case True
    ' first handle incorrect input
    Case (not IsNumeric(age))
        WshShell.SendKeys "You silly, you have to enter a number.{ENTER}"
    ' You can combine cases with a comma:
    Case (age<3), (age>120)
        WshShell.SendKeys "No, no. I don't think that is correct.{ENTER}"

    ' Now evaluate correct cases
    Case (age>24)
        WshShell.SendKeys "I see you are " & age - 24 & " years older than me.{ENTER}"
    Case (age<24)
        WshShell.SendKeys "I see you are " & 24 - age &" years younger than me.{ENTER}"
    Case (age=24)
        WshShell.SendKeys "Wow were the same age!{ENTER}"

    ' Alternatively you can use the Case Else to capture all rest cases
    Case Else
        ' But as all other cases handling the input this should never be reached for this snippet
        WshShell.SendKeys "Woah, how do you get here? The Universe Collapse Sequence is now initiated.{ENTER}"

End Select
Run Code Online (Sandbox Code Playgroud)

我添加了一些额外的案例来向您展示这款增强型交换机的强大功能.与If a And b Then陈述相反,逗号的情况是短路的.