欢呼声
for (int i = 0; i < movies.length; i++)
{
//RentalMovie movie = movies[i];
if(movies[i].getMovieGenre().equalsIgnoreCase("action") && movies[i].getMovieGenre().equalsIgnoreCase(genreID))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else if(movies[i].getMovieGenre().equalsIgnoreCase("childrens") && movies[i].getMovieGenre().equalsIgnoreCase(genreID))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else if (movies[i].getMovieGenre().equalsIgnoreCase("drama") && movies[i].getMovieGenre().equalsIgnoreCase(genreID))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else
{
genreList += "- No rental movies were found for the genre: " + genreID;
}
}
答案 0 :(得分:0)
可能发生的是if
语句未正确评估。这就是我想要的:
for (int i = 0; i < movies.length; i++)
{
//RentalMovie movie = movies[i];
if(movies[i].getMovieGenre().equalsIgnoreCase("action") && movies[i].getMovieGenreID().equalsIgnoreCase(genreID))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else if(movies[i].getMovieGenre().equalsIgnoreCase("childrens") && movies[i].getMovieGenreID().equalsIgnoreCase(genreID))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else if (movies[i].getMovieGenre().equalsIgnoreCase("drama") && movies[i].getMovieGenreID().equalsIgnoreCase(genreID))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else
{
genreList += "- No rental movies were found for the genre: " + genreID;
}
}
我要尝试的另一件事是摆脱一些代码,即:
for (int i = 0; i < movies.length; i++)
{
//RentalMovie movie = movies[i];
if(movies[i].getMovieGenre().equalsIgnoreCase("action"))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else if(movies[i].getMovieGenre().equalsIgnoreCase("childrens"))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else if (movies[i].getMovieGenre().equalsIgnoreCase("drama"))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else
{
genreList += "- No rental movies were found for the genres action, childrens, or drama -";
}
}
我建议的第三件事就是试试这个:
for (int i = 0; i < movies.length; i++)
{
//RentalMovie movie = movies[i];
if(movies[i].getMovieGenre().equalsIgnoreCase("action") || movies[i].getMovieGenre().equalsIgnoreCase(genreID))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else if(movies[i].getMovieGenre().equalsIgnoreCase("childrens"))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else if (movies[i].getMovieGenre().equalsIgnoreCase("drama"))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else
{
genreList += "- No rental movies were found for the genre: " + genreID;
}
}
答案 1 :(得分:0)
我怀疑你的意思是你在数组中有一些符合你搜索条件的条目,而其他条目没有。对于每个匹配,正在执行相应的if
子句,但对于每个不匹配,else
正在执行。
由于else中的操作是添加一条消息,表明找到了 no 匹配项,因此您可能不希望每次找到一个不匹配项时添加该匹配项。相反,如果没有找到任何匹配,你想在循环之后执行一次。这可能看起来像:
String genreList = "";
for (int i = 0; i < movies.length; i++)
{
if(movies[i].getMovieGenre().equalsIgnoreCase("action") && movies[i].getMovieGenre().equalsIgnoreCase(genreID))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else if(movies[i].getMovieGenre().equalsIgnoreCase("childrens") && movies[i].getMovieGenre().equalsIgnoreCase(genreID))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else if (movies[i].getMovieGenre().equalsIgnoreCase("drama") && movies[i].getMovieGenre().equalsIgnoreCase(genreID))
{
genreList += movies[i].getMovieID() + " - " + movies[i].getMovieTitle() + "[" + movies[i].getMediaType() + "]\n";
}
else
{
}
}
if (genreList.equals(""))
{
genreList = "No rental movies were found for the genre: " + genreID;
}
P.S。我完全不明白为什么你在接受它之前检查每部电影的类型genreID
和硬编码值。列表中是否存在其他类型,但您不希望用户能够搜索?这是它似乎有意义的唯一方式,但这似乎不是一个非常明智的要求。
答案 2 :(得分:0)
尝试使用调试器逐步执行它,您可以随时观察变量的状态。