[英]Check if list contains all words in string
好吧,我正在閱讀檢查字符串是否包含列表中的所有輸入並進行了嘗試,但是當我遇到諸如“誰發現了Austrlia”之類的基本問題時,如果我在答案中輸入了“ captain,cook”作為關鍵詞,它會說我錯了。
不知道我在做什么錯; 使用以下代碼:
GivenAnswer = textBox1.Text;
String invariantText = textBox1.Text.ToUpperInvariant();
bool matches = KeyWords.All(kw => invariantText.Contains(kw.ToUpperInvariant()));
if (matches)
{
correct++;
if (InstantResult) { MessageBox.Show("Questions Answered Correctly", "Answer Results", MessageBoxButtons.OK, MessageBoxIcon.Information); }
}
else
{
incorrect++;
if (InstantResult) { MessageBox.Show("Question Answered Wrong, sorry!", "Answer Result", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); }
}
Study_Helper.Form1.QuestionResults.Add(Question + "|" + (matches ? "true" : "false") + "|" + (Exact? "N/A" : KeyWords_Found()) + "|" + (Hint_Used ? "true" : "false") + "|" + GivenAnswer.ToLowerInvariant());
LoadUp();
textBox1.Clear();
textBox1.Focus();
出於調試目的,請確認KeyWords
和invariantText
包含期望值。 使用調試器或Console.WriteLine().
這是單元測試變得有價值的地方。 NUnit或MSTest可用於C#/ VS開發。
感謝對我的問題的第一條評論。 我親戚說,我從未真正清除過arraylist,請確保在執行此檢查后清除arraylist,以免在關鍵字列表中得到想要的更多值。
KeyWords.Clear();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.