如果其他,随机发生什么事怎么办?

时间:2016-06-03 03:59:28

标签: vbscript

我目前正在制作简单的AI(无论如何)。我有一堆If..Else来回应他们输入的内容但我正在寻找语法,当他们输入没有编程的内容时会出现MsgBox 。因此,如果他们输入类似的东西," dasdsafasfresxdf"在if中它不会运行MsgBox命令。

2 个答案:

答案 0 :(得分:2)

通常,Else分支用于处理与条件不匹配的内容。

If inputVar = "expected value A" Then
  'do some
ElseIf inputVar = "expected value B" Then
  'do other
ElseIf ...
  ...
Else
  MsgBox "Unexpected input."
End If

如果您总是在比较同一个变量,那么您也可以使用Select Case语句而不是多个ElseIf分支:

Select Case inputVar
  Case "expected value A"
    'do some
  Case "expected value B"
    'do other
  Case ...
    ...
  Case Else
    MsgBox "Unexpected input."
End Select

答案 1 :(得分:0)

您可以考虑Select Case声明:

Select Case(input)
 case "hi" : response = "hello"
 case "bye": response = "goodbye"
 '...etc
 case Else : MsgBox "Sorry, I didn't understand " & input & ".  Can you rephrase?"

End Select
' do whatever with the response here...