一个条件,多个事件(one condition, multiple events)

2019-08-19 03:07发布

我试图找出如何使多个事件发生使用.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

Answer 1:

我认为你正在寻找的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

我把一些额外的情况下,向您展示这种增强的开关电源。 在相反If a And b Then语句,用逗号的情况下被短路。



Answer 2:

封装在一个过程或功能的冗余代码。 另外一个不同的控制结构可能更适合那种你申请的检查:

If age>24 Then
  TypeResponse "I see you are " & (age-24) & " years older than me."
ElseIf age<24 Then
  TypeResponse "I see you are " & (24-age) & " years younger than me."
ElseIf age=24 Then
  TypeResponse "Wow were the same age!"
End If

Sub TypeResponse(text)
  WshShell.SendKeys text & "{ENTER}"
End Sub


文章来源: one condition, multiple events