我希望能够检查字符串是否包含列表中的所有值; 因此,如果您的答案列表中包含所有“关键词”,它只会给您一个“正确答案”。 这是我厌倦的事情,其中一半失败了;(不检查所有数组,只接受一个)。 代码我累了:
foreach (String s in KeyWords)
{
if (textBox1.Text.Contains(s))
{
correct += 1;
MessageBox.Show("Correct!");
LoadUp();
}
else
{
incorrect += 1;
MessageBox.Show("Incorrect.");
LoadUp();
}
}
基本上我想做的是:
Question: What is the definition of Psychology?
arraylist中的关键词:study,mental process,behaviour,humans
Answer: Psychology is the study of mental process and behaviour of humans
现在,当且仅当上面的答案包含所有关键词时,我的代码才会接受答案。 我希望我已经清楚这一点。
编辑:感谢大家的帮助。所有答案都已投票通过,我感谢大家的快速回答。我投票赞成可以轻松适应任何代码的答案。 :)
最佳答案
使用 LINQ:
// case insensitive check to eliminate user input case differences
var invariantText = textBox1.Text.ToUpperInvariant();
bool matches = KeyWords.All(kw => invariantText.Contains(kw.ToUpperInvariant()));
关于c# - 检查字符串是否包含列表中的所有输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14728294/