如何确保输入的内容与数组索引匹配,以便输出正确的解决方案?

时间:2018-11-30 03:21:08

标签: java arrays for-loop if-statement methods

因此,基本上,我正在尝试输出特定年份的顶级电影和该年电影的总收入。我的两种方法均无法正常工作,特别是在获取输入年份以匹配电影中所包含的年份方面,以输出该年的总收入和最佳影片时,我尤其遇到困难。

目前,我的程序输出如下:

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

3 个答案:

答案 0 :(得分:0)

max函数似乎存在错误。

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];
                max = f[i].getFilmIncome(); // you forget to set the value of max.
            }
        }
        return topFilm;
    }

答案 1 :(得分:0)

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, total.getFilmIncome());

    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; 
    double max = Double.MIN_VALUE; 
    for (int i = 0; i < f.length; i++) { 
        if (f[i].getPremiereYear() == yearEntered && max < f[i].getFilmIncome()) { 
            max = f[i].getFilmIncome(); totalIncome = f[i]; 
        } 
    } 
    return totalIncome; 
} 

private static Films getTopFilm(Films[] f, int yearEntered,double total) { 
    Films topFilm = null; 
    double max = 0; 
    for (int i = 0; i < f.length; i++) { 
        if (f[i].getPremiereYear() != yearEntered) { 
            continue; 
        } 
        if (f[i].getFilmIncome() == total) { 
            topFilm = f[i]; 
            break;
        } 
    } 

    return topFilm; 
}

答案 2 :(得分:0)

像Vivek一样,您在max函数中遇到问题

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

但是您的get getTotalIncome方法中也存在一个错误:

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