我正在尝试创建在搜索中找到最多显示 18 行电影信息(标题、年份、流派、质量、导演)的代码,但在控制台上显示代码时遇到问题。
相关结构:
struct Movie
{
public string title;
public int year;
public Name director;
public float quality;
public string mpaaRating;
public string genre;
public List<Name> cast;
public List<string> quotes;
public List<string> keywords;
}
struct MovieList
{
public int length;
public Movie[] movie;
}
有问题的代码如下:
static void searchMovies(ref MovieList ML, int menuChoice)
{
Movie searchTerm;
float searchRating;
int searchIndex;
int foundIndex;
Movie[] foundMovie = new Movie[NUM_MOVIES];
do
{
int.TryParse(searchMenu(), out menuChoice);
foundIndex = 0;
Console.WriteLine("Enter the movie title to search for");
searchTerm.title = Console.ReadLine();
for (searchIndex = 0; searchIndex < ML.length; searchIndex++)
{
if (ML.movie[searchIndex].title == searchTerm.title)
{
foundMovie[foundIndex].title = ML.movie[searchIndex].title.ToUpper();
foundMovie[foundIndex].year = ML.movie[searchIndex].year;
foundMovie[foundIndex].genre = ML.movie[searchIndex].genre;
foundMovie[foundIndex].mpaaRating = ML.movie[searchIndex].mpaaRating;
foundMovie[foundIndex].director.firstName = ML.movie[searchIndex].director.firstName;
foundMovie[foundIndex].director.lastName = ML.movie[searchIndex].director.lastName;
foundIndex++;
}
}
Console.Clear();
displaySearchResults(foundMovie, foundIndex);
}
static void displaySearchResults(Movie[] foundMovie, int foundIndex)
{
int displayedLines = 0;
int displayResults;
do
{
for (displayResults = 0; displayResults < foundMovie.Length; displayResults++)
{
Console.WriteLine("{0} {1} {2} {3} {4} {5}", foundMovie[displayResults].title, foundMovie[displayResults].year, foundMovie[displayResults].genre,
foundMovie[displayResults].mpaaRating, foundMovie[displayResults].director.firstName, foundMovie[displayResults].director.lastName);
displayedLines++;
}
Console.ReadLine();
} while (displayedLines < 18);
}
此代码应该检查 ML.movie[searchIndex].title 到 searchTerm,如果找到,则将 ML.movie[searchIndex] 的标题、年份、流派、评级和导演姓名(名字和姓氏)的值加载到在foundIndex 的当前值处的foundMovie 结构。然后,在搜索完整个电影列表后,进入 display 方法并打印出一行代码,其中包含前面提到的值(标题、年份、流派、评级和导演姓名)。
当我运行代码并完成搜索过程时(ML 结构预加载了二进制文件中的电影),当显示搜索结果时,我得到的只是一堵 0 的墙,如图所示.