【问题标题】:How can I make sure that the entered input matches the array index in order to output the right solution?如何确保输入的输入与数组索引匹配以输出正确的解决方案?
【发布时间】: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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:
      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; 
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-16
        • 2016-07-28
        • 1970-01-01
        • 1970-01-01
        • 2018-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多