有关读取txt文档并对其进行搜索的问题!
给一个包含如下列文档的的txt文件每行后面的数字代表 从1900 到2000 年的11 个数字
A 83 140 228 286 426 612 486 577 836 0 0
Aaliyah 0 0 0 0 0 0 0 0 0 380 215
Aaron 193 208 218 274 279 232 132 36 32 31 41
Abagail 0 0 0 0 0 0 0 0 0 0 958
Abbey 0 0 0 0 0 0 0 0 537 451 428
。。。。。。
输出结果类似这样
请输入一个名字:A
字母为:A
第一次出现次数:83
第二次出现次数:140
第三次出现次数:228
已经分出4个array,但是查询方程不会了,下面是我的code,请帮忙看一下,谢谢
using System;
namespace FirstName
{
class MainClass
{
static void ReadData(string filename, out string[] Names,
out string[] Decade1980, out string[] Decade1990, out string[] Decade2000)
{
string input;
file;
file = new (filename);
input = file.ReadLine(); // open file, read first line to get N, parse it:
int N;
N = int.Parse (input);
Names = new string[N]; // create the arrays:
Decade1980 = new string[N];
Decade1990 = new string[N];
Decade2000 = new string[N];
input = file.ReadLine();
for (int i = 0; i < N; i++)
{
string[] values;
values = input.Split ();
Names [i] = values[0];
Decade1980[i] = values[9];
Decade1990[i] = values[10];
Decade2000[i] = values[11];
input = file.ReadLine();
}
file.Close ();
}
static void IsNameExist(string word, string[] Names, string[] Decade1980, string[] Decade1990,
string[] Decade2000)
{
int i;
i = 0;
while (i < Names.Length)
{
if (Names [i] == word)
{
Console.WriteLine ("Name: " + Names [i]);
Console.WriteLine ("Popularity in 1980: " + Decade1980 [i]);
Console.WriteLine ("Popularity in 1990: " + Decade1990 [i]);
Console.WriteLine ("Popularity in 2000: " + Decade2000 [i]);
}
else
{
Console.WriteLine (">> Search failed: " + "'" + word + "'" + "was not found");
}
}
}
static void Main(string[] args)
{
Console.WriteLine ("** Name Popularity Program **");
Console.WriteLine ();
string[] Names;
string[] Decade1980;
string[] Decade1990;
string[] Decade2000;
ReadData ("firstnames.txt", out Names,
out Decade1980, out Decade1990, out Decade2000);
string word;
Console.Write (">>Please enter a first name to search for: ");
word = Console.ReadLine ();
IsNameExist (word, Names, Decade1980, Decade1990, Decade2000);
Console.Write (">>Please enter a first name to search for: ");
word = Console.ReadLine ();
Console.WriteLine ();
Console.WriteLine ("** Bye **");
}
}
}