我对Java还是很陌生,有人可以解释一下,当我输入一个满足代码不断循环回到初始for循环要求(1-10)的值时吗?我如何修改代码以解决问题并允许其正常运行?

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);
            }
        }
    }
}

最佳答案

移动这条线

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时,该方法将立即存在。

10-01 22:20