【发布时间】:2021-12-14 09:56:37
【问题描述】:
在我正在制作一个“博客”的项目中工作,该项目将博客文章放入一个字符串数组,然后再传输到一个列表数组。这是我当前的代码,我将不胜感激。我对 c# 和整个编程还是很陌生,所以不确定如何解决它。
当前的问题是,在案例 3 中,我收到错误消息:运算符 '==' 不能应用于 'string' 和 'string[]' 类型的操作数,并且名称 "i" 在当前不存在“帖子在博客中”代码块的上下文。
我在这里创建了一个最小的可重现示例,该程序唯一的其他功能是 1. 删除所有博客文章和 2. 打印所有博客文章及其名称和内容。
bool minBool = true;
List<string[]> blogPost = new List<string[]> { };
string[] post = new string[2];
while (minBool)
{
Console.WriteLine("\n\n\tWelcome to the blog!");
Console.WriteLine("\n\t[1] - Write a blogpost");
Console.WriteLine("\t[3] - Search for a blogpost");
Console.Write("\n\tChoice:");
Int32.TryParse(Console.ReadLine(), out int input);
switch (input)
{
case 1:
Console.Write("\tName your post: ");
post = new string[2];
post[0] = Console.ReadLine();
Console.Write("\tWrite your post: ");
post[1] = Console.ReadLine();
blogPost.Add(post);
break;
case 3:
string searchTerm = Console.ReadLine();
string result = "The blogpost doesn't exist";
foreach (string blog in blogPost)
{
if (searchTerm == blog)
{
result = $"\tThe post is in the blog: {post[i]}";
break;
}
}
Console.WriteLine(result);
break;
【问题讨论】: