我正在尝试创建一个代码,在搜索中找到它后会显示多达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结构。然后,在搜索整个电影列表后,进入显示方法并打印出一行代码,包括前面提到的值(标题,年份,流派,评级和导演名称)。
当我运行代码并完成搜索过程(ML结构预装了二进制文件中的电影)时,当需要显示搜索结果时,我得到的只是一个0的墙,如图所示在图片中。
答案 0 :(得分:0)
即使与displaySearchResults
0不匹配,您当前也会调用foundIndex
- 然后您继续显示数组中的第一个项目,该项目从未设置过,因此显示所有默认值 - 空字符串和零(假设您也为其他结构使用struct
)。
两个建议:
class
而不是struct
- 这似乎更合适。Movie
数组,请使用List<Movie>
发现电影,因为你不知道会有多少电影匹配。