我正在试图弄清楚如何制作一个交互式单词搜索控制台应用程序。 应用程序在文本文件中搜索用户输入的字符,并输出包含输入字中字符的所有单词。 它应该工作的方式是用户输入搜索条目,控制台显示结果AS用户键入字符,在每个新字符后更新。
我现在的工作方式要求用户按Enter键进行搜索。我需要改变什么?
public class Search
{
public static void Main()
{
const string PATH = "kotus_sanat.txt";
string[] words = File.ReadAllLines(PATH);
Console.Write("search: ");
string entry = Console.ReadLine();
List<string> result = new List<string>();
result= FindWords(entry, words);
for (int i = 0; i < result.Count; i++)
{
Console.WriteLine(result[i]);
}
}
public static List<string> FindWords(string entry, string[] words)
{
List<string> results = new List<string>();
for (int i = 0; i < words.Length; i++)
{
if (ContainsAllChars(words[i], entry) == true) results.Add(words[i]);
}
return results;
}
public static bool ContainsAllChars(string word, string chars)
{
StringBuilder charsNow = new StringBuilder(chars);
StringBuilder wordNow = new StringBuilder(word);
int i = 0;
while (i < chars.Length)
{
char charNow = chars[i];
int charIndex = wordNow.IndexOf(charNow);
if (charIndex >= 0)
{
wordNow[charIndex] = (char)0;
}
else return false;
i++;
}
return true;
}
}
public static class Helper
{
public static int IndexOf(this StringBuilder sb, char c, int startIndex = 0)
{
for (int i = startIndex; i < sb.Length; i++)
if (sb[i] == c)
return i;
return -1;
}
}