【发布时间】:2018-11-30 03:21:07
【问题描述】:
所以基本上,我试图输出特定年份的顶级电影和电影当年的总收入。我的两种方法都不能正常工作,我特别难以让输入的年份与数组中电影的年份相匹配,以便输出当年的总收入和顶级电影。
现在我的程序输出如下:
** 输入 1991 - 2018 的年份:2016
感谢您收到 2016 年
2016 年的总金额为 966.50 美元
2016 年电影收入最高的是《丛林之书》,966.50 美元**
但是,2016 年的总金额不是 966.50 美元,而且 2016 年排名靠前的电影不是《丛林之书》,而是《海底总动员》……
这里是getInput.getUserInput()
public static int getUserInput(int minYear, int maxYear) {
int year = 0;
boolean keepLooping = true;
Scanner input = new Scanner(System.in);
while (keepLooping) {
System.out.printf("\nEnter a year from %s - %s:", minYear, maxYear);
year = input.nextInt();
if (year < minYear || year > maxYear) {
System.out.printf("Invalid entry . . .");
} else {
keepLooping = false;
}
}
return year;
}
这是课程
public class Films {
private String filmTitle;
private double filmIncome;
private int premiereYear;
Films(String title, double income, int year) {
filmTitle = title;
filmIncome = income;
premiereYear = year;
}
public String getFilmTitle() {
return filmTitle;
}
public void setFilmTitle(String filmTitle) {
this.filmTitle = filmTitle;
}
public double getFilmIncome() {
return filmIncome;
}
public void setFilmIncome(double filmIncome) {
this.filmIncome = filmIncome;
}
public int getPremiereYear() {
return premiereYear;
}
public void setPremiereYear(int premiereYear) {
this.premiereYear = premiereYear;
}
}
这是运行程序的文件
public static void main(String[] args) {
Films[] f = new Films[8];
f[0] = new Films("Frozen", 1290.0, 2013);
f[1] = new Films("The Lion King", 968.4, 1994);
f[2] = new Films("Zootopia", 1023.7, 2016);
f[3] = new Films("Incredibles 2", 1240.3, 2018);
f[4] = new Films("Finding Dory", 1028.5, 2016);
f[5] = new Films("Shrek 2", 919.8, 2004);
f[6] = new Films("The Jungle Book", 966.5, 2016);
f[7] = new Films("Despicable Me 2", 970.7, 2013);
int yearEntered = getInput.getUserInput(1991, 2018);
System.out.printf("\nThank you received %s", yearEntered);
Films total = getTotalIncome(f, yearEntered);
System.out.printf("\nThe total amount made for %s was $%2.2f", yearEntered, total.getFilmIncome());
Films top = getTopFilm(f, yearEntered);
if (top == null) {
System.out.printf("0");
} else {
System.out.printf("\nThe greatest income made by a movie in %s was %s at $%2.2f", yearEntered,
top.getFilmTitle(), top.getFilmIncome());
}
}
private static Films getTotalIncome(Films[] f, int yearEntered) {
Films totalIncome = null;
double add = 0;
for (int i = 0; i < f.length; i++) {
if (f[i].getPremiereYear() == yearEntered) {
add += f[i].getFilmIncome();
totalIncome = f[i];
}
}
return totalIncome;
}
private static Films getTopFilm(Films[] f, int yearEntered) {
Films topFilm = null;
double max = 0;
for (int i = 0; i < f.length; i++) {
if (f[i].getPremiereYear() != yearEntered) {
continue;
}
if (f[i].getFilmIncome() > max) {
topFilm = f[i];
}
}
return topFilm;
}
【问题讨论】:
-
特定年份,但您要进入 两年 年?
-
@zlakad 输入应在 1991 和 2018 的范围内。我输入的是 2016。因此,“谢谢您收到 2016。
-
你在
getTotalIncome方法中有一些逻辑错误。因为它被编码为返回列表中的最后一部电影,而您想要的值被计算并存储在变量add中。同样在getTopFilm方法中,变量max始终为0,因此也只会返回列表中的最后一部电影。希望这能为您指明正确的方向。
标签: java arrays for-loop if-statement methods