首页 > 解决方案 > 如何确保输入的输入与数组索引匹配以输出正确的解决方案?

问题描述

所以基本上,我试图输出特定年份的顶级电影和电影当年的总收入。我的两种方法都无法正常工作,我特别难以让输入的年份与数组中电影的年份相匹配,以便输出当年的总收入和顶级电影。

现在我的程序输出如下:

** 输入 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;
    }

标签: javaarraysfor-loopif-statementmethods

解决方案


就像 Vivek 说你的最大功能有问题

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].getFilmIncome() > max) {
            topFilm = f[i];
            //need to reset max here
            max = f[i].getFilmIncome();
        }
    }
    return topFilm;
}

但是您的 getgetTotalIncome方法中也有一个错误:

private static double getTotalIncome(Films[] f, int yearEntered) {
    double total = 0;
    for (int i = 0; i < f.length; i++) {
        if (f[i].getPremiereYear() == yearEntered) {
            total += f[i].getFilmIncome();
        }
    }
    return total;
}

我不知道你为什么Film要从那里返回一个对象,你想做的是遍历那些总结当年所有电影的电影。

然后你会编辑你的 main 函数来调用它

public static void main(String[] args) {
    ...
    double total = getTotalIncome(f, yearEntered);
    System.out.printf("\nThe total amount made for %s was $%2.2f", yearEntered, total);
    ...
}

推荐阅读