我正在写一本日志作为学校的作业,我已经完成了如下所示。尽管我正在努力解决一些需要实现的事情,但我最关心的是理解为什么我需要列表中的一个数组。
我被告知使用:
List logBook = new List { };
string[]post = new string [2]
功能是我想能够保存新帖子,至少有一个标题和一条消息。日志应该是一个列表,每个日志应该是一个数组。
所以我的问题只是我是否朝着正确的方向前进,有人可以帮助我理解为什么它必须是列表中的一个数组。另外,当涉及到搜索部分时,请随时帮助我朝正确的方向前进,我希望能够在帖子中搜索日期、标题或单个单词。
static void Main(string[] args) {
string titel;
string post;
string[] logg = new string[20];
List<string[]> logBook = new List<string[]> { };
DateTime tiden = DateTime.Now;
Console.WriteLine(tiden.ToShortDateString());
bool go = true;
while (go)
try
{
Console.WriteLine("\n\tWelcome to the Logbook!\n");
Console.WriteLine("\t[1] Write a new post");
Console.WriteLine("\t[2] Search for a post");
Console.WriteLine("\t[3] Display all posts");
Console.WriteLine("\t[4] Quit");
Console.Write("\n\tSelect from menu: ");
int menyVal = Convert.ToInt32(Console.ReadLine());
int i = 0;
switch (menyVal)
{
case 1:
Console.WriteLine("\tWrite a title to your post: ");
titel= Console.ReadLine();
Console.WriteLine("\n\tWrite your post: ");
Console.WriteLine("\t" + tiden.ToShortDateString() + "\n");
Console.WriteLine("\t" + titel + "\t");
post = Console.ReadLine();
logg[i] = tiden.ToShortDateString() + "\n" + titel + "\n" + post + "\n";
logBook.Add(logg);
i = i + 1;
break;
case 2:
Console.WriteLine("\tWrite a searchword or a date (yyyy-mm-dd)");
string keyword = Console.ReadLine();
foreach (var item in logBook)
{
if (logg[i] == keyword)
Console.WriteLine(logg[i]);
else
Console.WriteLine("Searchword couldn't be found.");
}
break;
case 3:
Console.WriteLine("\tThese are the current posts in Logbook.\n ");
foreach (string[] element in logBook)
{
Console.WriteLine("\t" + element);
}
break;
default:
Console.WriteLine("\tChoose from menu 1 - 4");
break;
case 4:
go = false;
break;
}
}
catch
{
Console.WriteLine("\tChoose from menu 1 - 4");
}
}
}