If 语句不能正常工作
If Statement not Working right
我需要 If 语句中一些运算符的帮助...
我是运行这样的代码
if (statement.Contains("weather") &&
(
statement.Contains("what") || statement.Contains("how")
)
)
{
statement = ("It's " + Weather.Get_Weather("condition") + " outside");
}
但它不起作用...任何人都可以帮助我解决这个问题,因为我在这段代码中找不到任何错误,而且我什至没有像 ||
这样的运算符的经验和 &&
因为我可以在 If Statements
.
中使用 If Statements
而且我希望声明包含天气和内容或方式,以便我可以确认这是一个问题,或者用户正在询问天气...
你也可以给我更多关于这个的想法...
'how is the weather'
这不是通过 If statement
它包含足够的方式和天气...
我真的很抱歉我的问题,这是我的问题我不知道 Contains()
区分大小写...
假设您正在测试的语句实际上是 'How is the weather' 那么您的 if statement
将按预期工作。您的检查是查看语句是否包含 'weather' 和 'what' 或包含单词 'how'(注意小写)。
由于您的短语不包含单词 'what',所以第一次检查(对于单词 'weather' AND 'what')将是错误的。此外,由于单词 'How' 以大写字母 'H' 开头,因此它不会与 'how' 匹配,因此 return 也会为 false,因此不会输入 if statement
.
如果您希望搜索不区分大小写,那么您还需要考虑语言,因为在某些语言中全大写的词与全小写的同一个词含义不同。这是一个类似的 question and answer,接受的答案在下面详细说明了完整性:
To test if the string paragraph contains the string word (thanks
@QuarterMeister) culture.CompareInfo.IndexOf(paragraph, word,
CompareOptions.IgnoreCase) >= 0
Where culture is the instance of CultureInfo describing the language
that the text is written in.
This solution is transparent about the definition of
case-insensitivity, which is language dependent. For example, the
English language uses the characters I and i for the upper and lower
case versions of the ninth letter, whereas the Turkish language uses
these characters for the eleventh and twelfth letters of its 29
letter-long alphabet. The Turkish upper case version of 'i' is the
unfamiliar character 'İ'.
Thus the strings tin and TIN are the same word in English, but
different words in Turkish. As I understand, one means 'spirit' and
the other is an onomatopoeia word. (Turks, please correct me if I'm
wrong, or suggest a better example)
To summarise, you can only answer the question 'are these two strings
the same but in different cases' if you know what language the text is
in. If you don't know, you'll have to take a punt. Given English's
hegemony in software, you should probably resort to
CultureInfo.InvariantCulture, because it'll be wrong in familiar ways.
我需要 If 语句中一些运算符的帮助...
我是运行这样的代码
if (statement.Contains("weather") &&
(
statement.Contains("what") || statement.Contains("how")
)
)
{
statement = ("It's " + Weather.Get_Weather("condition") + " outside");
}
但它不起作用...任何人都可以帮助我解决这个问题,因为我在这段代码中找不到任何错误,而且我什至没有像 ||
这样的运算符的经验和 &&
因为我可以在 If Statements
.
If Statements
而且我希望声明包含天气和内容或方式,以便我可以确认这是一个问题,或者用户正在询问天气... 你也可以给我更多关于这个的想法...
'how is the weather'
这不是通过 If statement
它包含足够的方式和天气...
我真的很抱歉我的问题,这是我的问题我不知道 Contains()
区分大小写...
假设您正在测试的语句实际上是 'How is the weather' 那么您的 if statement
将按预期工作。您的检查是查看语句是否包含 'weather' 和 'what' 或包含单词 'how'(注意小写)。
由于您的短语不包含单词 'what',所以第一次检查(对于单词 'weather' AND 'what')将是错误的。此外,由于单词 'How' 以大写字母 'H' 开头,因此它不会与 'how' 匹配,因此 return 也会为 false,因此不会输入 if statement
.
如果您希望搜索不区分大小写,那么您还需要考虑语言,因为在某些语言中全大写的词与全小写的同一个词含义不同。这是一个类似的 question and answer,接受的答案在下面详细说明了完整性:
To test if the string paragraph contains the string word (thanks @QuarterMeister) culture.CompareInfo.IndexOf(paragraph, word, CompareOptions.IgnoreCase) >= 0
Where culture is the instance of CultureInfo describing the language that the text is written in.
This solution is transparent about the definition of case-insensitivity, which is language dependent. For example, the English language uses the characters I and i for the upper and lower case versions of the ninth letter, whereas the Turkish language uses these characters for the eleventh and twelfth letters of its 29 letter-long alphabet. The Turkish upper case version of 'i' is the unfamiliar character 'İ'.
Thus the strings tin and TIN are the same word in English, but different words in Turkish. As I understand, one means 'spirit' and the other is an onomatopoeia word. (Turks, please correct me if I'm wrong, or suggest a better example)
To summarise, you can only answer the question 'are these two strings the same but in different cases' if you know what language the text is in. If you don't know, you'll have to take a punt. Given English's hegemony in software, you should probably resort to CultureInfo.InvariantCulture, because it'll be wrong in familiar ways.