其他答案已经解释了您的逻辑是倒退的,就是这样。
而且,我不知道您是否准确地复制了您的代码,但是当我将它复制到 VB 中时,我得到 - 正如我所料 - 带有 MsgBox 行的语法错误。我可以通过在MsgBox 参数中添加括号来“修复”这个问题,例如:wordFound = True And MsgBox("Word is in this") 等。但这不是好的代码,原因我将解释,我还有其他一些建议。
考虑对您的代码进行这些更改:
Private Sub conversation(theInput As String)
Dim arrWords As String, aWord As String
arrWords = Array("good", "great", "bad")
Dim wordFound As Boolean
wordFound = False
For Each aWord In arrWords
If InStr(theInput, aWord) = 0 Then
wordFound = False
MsgBox """" & aWord & """ is in this"
Else
wordFound = True
MsgBox """" & aWord & """ is not in this"
End If
Next
End Sub
Private Sub SendButton_Click()
conversation(myChatTextBox.Text)
End Sub
好的。这里有几点。
- 请勿使用
Variant,除非您有令人信服的理由这样做。这是存储信息效率最低的方法,因为它必须分配额外的内存以在内部告诉它是什么类型的变量,并且它还必须分配足够的内存来包含可能的最大类型。 (String 变量有 10 个字节加上字符串中每个字符一个,而分配为字符串的Variant 类型有 22 个字节加上字符串中每个字符一个。)
- 我将
imput 更改为theInput。 input是VB中的保留字,所以不能用,但是不要拼错,别人会更清楚。最好找个前缀放在上面。
- 正如其他人所说,当
InStr 返回零时,这意味着参数 2 中的字符串不在参数 1 中的字符串中。因此,这意味着“单词不在其中”,而不是那个它是。 (这就是您遇到的问题的答案;剩下的只是为了改进您的代码。)
-
wordFound = True And MsgBox("Word is in this") 只是巧合。 MsgBox 在没有参数的情况下成功运行时返回值 1。 (谁知道?我不得不自己尝试一下。可能是因为它可以设置为针对不同类型的 ms 返回许多不同的值)所以你的逻辑是wordFound = True And 1。 And 是一个逻辑比较运算符:True And 1 的计算结果为 True,而 False And 1 的计算结果为 False。所以,你得到了你想要的,但几乎是偶然的。将两个代码位放在两条不同的行上。您无需在逻辑上比较它们;事实上,这样做是没有意义的。 (如果您实际上想将两行代码放在同一行上,请在它们之间加一个冒号:wordFound = True : MsgBox "etc",但这通常不被认为是好的做法,因为代码的可读性较差。我有你认为的感觉使用And 来执行此操作,正如您所看到的,它做了一些完全不同的事情。)
- 我更改了您的消息,在引号中包含您要查找的单词,例如
"good" is in this。要在字符串中获取文字引号,请使用其中两个:""。 (您必须将两个引号本身放在引号中,因为它们是带引号的字符串;这就是为什么开头有四个,后面有三个。)
- 这里不需要
ElseIf,因为如果您的If 条件为假,那么您的ElseIf 条件为真。如果您要评估两个以上的可能条件,则只需要 ElseIf。
- 我已经建立了如何将聊天框用户的输入发送到您的
conversation 子例程的基本思想。当用户单击Send 按钮时,您将文本框的内容作为input 参数发送到conversation。如果将其设置为局部变量,则必须编写某种代码来获取用户的输入。这是完成这项工作的更清洁的方式。
综上所述,您可以像这样进一步简化 For Each 循环:
For Each aWord In arrWords
wordFound = InStr(input, aWord) > 0
MsgBox """" & aWord & """ is" & IIf(wordFound, "", " not") & " in this"
Next
解释:
-
InStr(input, aWord) <> 0 为真或假。您将其分配给wordFound。这是处理If...Else 的更简洁的方式。 (这个想法的一个更简单的例子:x = 1 = 1 将设置x 等于True,而x = 1 = 0 将设置x 等于false。您可以使用括号使其更容易理解:@987654363 @等)
-
IIf ("instant if") 采用 IIf(condition, valueIfConditionIsTrue, valueIfConditionIsFalse) 的形式。
- 因为
wordFound是一个布尔值,所以说wordFound和说wordFound = True是一样的,所以你可以省略= True(你也可以说Not wordFound和wordFound = False意思一样)。
编辑:如果您希望聊天机器人在遇到任何单词时回复“太好了”,请将 conversation() 从 Sub 更改为 Function 并返回 true 或 false。像这样:
Private Function conversation(theInput As String) As Boolean
Dim arrWords As String, aWord As String
arrWords = Array("good", "great", "bad")
'Get rid of your wordFound variable — you don't need it any more
conversation = False
For Each aWord In arrWords
If InStr(theInput, aWord) > 0 Then
conversation = True
End If
Next
End Function
Private Sub SendButton_Click()
If conversation(myChatTextBox.Text)
MsgBox "That's great!"
Else
MsgBox "That's not so great."
End If
End Sub