我仍然是java的新手,有人可以解释当我输入符合要求(1-10)的值时,代码会循环回到初始for循环吗?我如何修改代码来解决问题并允许正常运行?
public void rateEpisode(Scanner sc, String seriesName, int searchEpisodeNumber, ArrayList<TVSeries> tvSeries) {
for(int i = 0; i<tvSeries.size(); i++) {
for(int j = 0; j< tvSeries.get(i).getListOfEpisodes().size(); j++){
if((seriesName.equals(tvSeries.get(i).getTitle())) &&
(searchEpisodeNumber == tvSeries.get(i).getListOfEpisodes().get(j).getEpisodeNumber())){
System.out.println("Please enter your rating(1-10) of " + tvSeries.get(i).getTitle() + ", Episode " + tvSeries.get(i).getListOfEpisodes().get(j).getEpisodeNumber() + ". "
+ tvSeries.get(i).getListOfEpisodes().get(j).getEpisodeName() + " : ");
boolean validInput = false;
int userEpRating = -1;
do{
System.out.println("Test");
validInput = false;
if(sc.hasNextInt()){
userEpRating=sc.nextInt();
sc.nextLine();
if(userEpRating < 11 && userEpRating > 0){
validInput = true;
} else{
System.out.println("Please enter a rating between 1 and 10: ");
sc.nextLine();
}
}else{
System.out.println("Please enter an integer between 1 and 10: ");
sc.nextLine();
}
}while(!validInput);
tvSeries.get(i).getListOfEpisodes().get(j).setUserEpReview(userEpRating);
}
}
}
}
答案 0 :(得分:0)
移动此行
tvSeries.get(i).getListOfEpisodes().get(j).setUserEpReview(userEpRating);
到这里:
if(userEpRating < 11 && userEpRating > 0){
validInput = true;
tvSeries.get(i).getListOfEpisodes().get(j).setUserEpReview(userEpRating);
return;
}
...
并在其后添加return
。
当您在不返回任何内容的方法(return
)中调用void
时,该方法立即存在。