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