我有两个文本文件匹配字符串[]:有C#控制台应用程序,搜索来自两个文本文件
number,letter,color,animal
1,a,green,alligator
2,b,brown,bear
3,c,black,cat
4,d,white,dog
5,e,pink,elephant
有
colour,animal,found,diet
green,alligator,swamp,fish
green,alligator,swamp,bird
brown,bear,forest,fruit
black,cat,home,catfood
white,dog,home,dogfood
pink,elephant,space,spacefruit
我的代码animal.txt和habit.txt到目前为止要求一个号码和一封信。并使用字符串[]和分割搜索文本文件。
class Program
{
static void Main(string[] args)
{
int counter = 0;
string line;
string number;
string letter;
bool lineFound = false;
do
{
Console.WriteLine("Enter number");
number = Console.ReadLine();
Console.WriteLine("\nEnter letter");
letter = Console.ReadLine();
System.IO.StreamReader file = new System.IO.StreamReader("animal.txt");
while ((line = file.ReadLine()) != null)
{
string[] words = line.Split(',');
if ((number == words[1]) && (letter == words[0]))
{
Console.WriteLine(line);
lineFound = true;
}
counter++;
}
if (!lineFound)
{
Console.WriteLine("Invalid number and/or letter");
}
file.Close();
}
while (!lineFound);
取决于输入它会显示颜色和动物的行。 如何制作它,以便它将搜索另一个文件,habit.txt以查找animal.txt中的匹配行。例如输入可以是“1”和“a”,控制台将显示
green,alligator,swamp,fish
green,alligator,swamp,bird