英文:
How to write a program in C#, which searches through object list by any of the movie data (using loops)?
问题
我正在使用C#创建一个简单的控制台应用程序,该应用程序具有使用循环搜索电影库(对象列表)中的任何电影数据(数据类型)的能力。
public static List<Movie> Movies= new List<Movie>()
{
(1, "The Shawshank Redemption", "Drama", 1994, "Tim Robbins", "9.3/10")
(2, "The Godfather", "Crime", 1972, "Marlon Brando", "9.2/10")
}
尝试一些方法但失去了主意:
public static void Search()
{
Console.WriteLine("输入搜索值: ");
string userInput = Console.ReadLine();
userInput.ToLower();
//foreach (var m in movies)
//{
// if
// (f.ID.ToString().Contains(userInput.ToLower()) || f.Title.Contains(userInput.ToLower()) || f.Year.ToString().Contains(userInput.ToLower()) || f.Genre.Contains(userInput.ToLower()) || f.mainActor.Contains(userInput.ToLower()) || f.IMDB_Rating.Contains(userInput.ToLower()))
foreach (Film f in movies)
{
if(f.ToString().Contains(userInput))
{
Console.WriteLine("找到值!\n");
Console.WriteLine($"{f.MovieForPrintOut}");
}
}
}
已经花了几个小时在YouTube、StackOverflow、OpenAI等网站上寻找最佳解决方案。
提前感谢,问候。
英文:
I am doing simple console app in C# which has ability to search through movie library (object list) by any movie data (data type) using loop.
public static List<Movie> Movies= new List<Movie>()
{
(1, "The Shawshank Redemption", "Drama", 1994, "Tim Robbins", "9.3/10")
(2, "The Godfather", "Crime", 1972, "Marlon Brando", "9.2/10")
}
Try smth but lost idea:
public static void Search()
{
Console.WriteLine("Enter input value: ");
string userInput = Console.ReadLine();
userInput.ToLower();
//foreach (var m in movies)
//{
// if
// (f.ID.ToString().Contains(userInput.ToLower()) || f.Title.Contains(userInput.ToLower()) || f.Year.ToString().Contains(userInput.ToLower()) || f.Genre.Contains(userInput.ToLower()) || f.mainActor.Contains(userInput.ToLower()) || f.IMDB_Rating.Contains(userInput.ToLower()))
foreach (Film f in movies)
{
if(f.ToString().Contains(userInput))
{
Console.WriteLine("Value find!\n");
Console.WriteLine($"{f.MovieForPrintOut}");
}
}
Already spent hours looking for best solution on youtube, stackoverflow, openAI, etc.
Thanks in advance,
greeting.
答案1
得分: 2
如果我理解正确,我认为您所陈述的想法类似下面的代码。
请注意,在某些情况下,我们可能希望进行精确匹配,比如对于 Year
或 Id
。否则,没有不区分大小写的 Contains
方法,但 IndexOf
接受一个比较器,因此我们可以使用该方法以不区分大小写的方式检查部分匹配(如果找到匹配的字符串,它会返回该字符串的索引,否则返回 -1
)。
// 采用用于查找匹配的电影列表的字符串
// 返回电影列表
public static List<Movie> Search(string input)
{
List<Movie> matches = new List<Movie>();
foreach (var movie in Movies)
{
if (movie.Id.ToString().Equals(input) ||
movie.Title.IndexOf(input, StringComparison.OrdinalIgnoreCase) >= 0 ||
movie.Year.ToString().Equals(input) ||
movie.Genre.IndexOf(input, StringComparison.OrdinalIgnoreCase) >= 0 ||
movie.MainActor.IndexOf(input, StringComparison.OrdinalIgnoreCase) >= 0 ||
movie.IMDBRating.IndexOf(input, StringComparison.OrdinalIgnoreCase) >= 0)
{
matches.Add(movie);
}
}
return matches;
}
示例用法:
List<Movie> crimeMovies = Search("Crime");
这应该能让您入门,但结果可能有点不可预测。即使用户想要的是流派 "Crime",任何名称中包含 "Crime" 的电影都将被返回,所以您可能会得到一部名为 "热情的罪行" 的浪漫喜剧的结果。这是需要考虑的事情...
英文:
If I understand correctly, I think the idea you were stating was something like the code below.
Note that in some cases we probably want to do an exact match, like for Year
or Id
. Otherwise, there is no case-insensitive Contains
method, but IndexOf
takes a comparer, so we can use that method to check for partial matches in a case-insensitive way (it returns the index of the string to match if it was found, otherwise -1
).
// Takes in a string that's used to find a list of movies that match
// The list of movies is returned
public static List<Movie> Search(string input)
{
List<Movie> matches = new List<Movie>();
foreach (var movie in Movies)
{
if (movie.Id.ToString().Equals(input) ||
movie.Title.IndexOf(input, StringComparison.OrdinalIgnoreCase) >= 0 ||
movie.Year.ToString().Equals(input) ||
movie.Genre.IndexOf(input, StringComparison.OrdinalIgnoreCase) >= 0 ||
movie.MainActor.IndexOf(input, StringComparison.OrdinalIgnoreCase) >= 0 ||
movie.IMDBRating.IndexOf(input, StringComparison.OrdinalIgnoreCase) >= 0)
{
matches.Add(movie);
}
}
return matches;
}
Sample usage:
List<Movie> crimeMovies = Search("Crime");
This should get you started, but the results will be a little unpredictable. Even though the Genre "Crime" was what the user wanted, any movie with "Crime" in the name will be returned, so you might get a Romantic Comedy named "Crimes of Passion" in the results. Something to think about...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论