0

我正在尝试创建在搜索中找到最多显示 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.movi​​e[searchIndex].title 到 searchTerm,如果找到,则将 ML.movi​​e[searchIndex] 的标题、年份、流派、评级和导演姓名(名字和姓氏)的值加载到在foundIndex 的当前值处的foundMovie 结构。然后,在搜索完整个电影列表后,进入 display 方法并打印出一行代码,其中包含前面提到的值(标题、年份、流派、评级和导演姓名)。

当我运行代码并完成搜索过程时(ML 结构预加载了二进制文件中的电影),当显示搜索结果时,我得到的只是一堵 0 的墙,如图所示.

运行时的示例输出

4

1 回答 1

0

displaySearchResults即使没有与 0 匹配,您当前也调用foundIndex- 然后您继续显示数组中的第一项,该数组从未设置过,因此显示所有默认值 - 空字符串和零(假设您struct将其他结构用作出色地)。

两个建议:

  • class不使用struct- 这里似乎更合适。
  • 不要使用固定大小的Movie数组,将 aList<Movie>用于找到的电影,因为您不知道将匹配多少部电影。
于 2012-12-02T03:43:46.397 回答