Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何确保输入的输入与数组索引匹配,以便输出正确的解决方案?_Java_Arrays_For Loop_If Statement_Methods - Fatal编程技术网

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

Java 如何确保输入的输入与数组索引匹配,以便输出正确的解决方案?,java,arrays,for-loop,if-statement,methods,Java,Arrays,For Loop,If Statement,Methods,所以基本上,我在努力输出某一年的最佳影片和该年电影的总收入。我的两种方法都不能正常工作,我尤其难以将输入的年份与阵列中电影的年份相匹配,以便输出当年的总收入和顶级电影 目前,我的程序输出如下: ** 输入1991年至2018年的年份:2016年 2016年收到的感谢信 2016年的总金额为966.50美元 2016年一部电影的最大收入是《丛林之书》,966.50美元** 然而,2016年的总金额不是966.50美元,2016年的顶级电影不是《丛林之书》,而是《寻找多莉》 下面是getInput.

所以基本上,我在努力输出某一年的最佳影片和该年电影的总收入。我的两种方法都不能正常工作,我尤其难以将输入的年份与阵列中电影的年份相匹配,以便输出当年的总收入和顶级电影

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

** 输入1991年至2018年的年份:2016年

2016年收到的感谢信

2016年的总金额为966.50美元

2016年一部电影的最大收入是《丛林之书》,966.50美元**

然而,2016年的总金额不是966.50美元,2016年的顶级电影不是《丛林之书》,而是《寻找多莉》

下面是
getInput.getUserInput()

这是运行该程序的文件

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;
    }
publicstaticvoidmain(字符串[]args){
电影[]f=新电影[8];
f[0]=新电影(“冻结”,1290.012013);
f[1]=新电影(《狮子王》,968.41994);
f[2]=新电影(“Zootopia”,1023.7,2016);
f[3]=新电影(《不可思议的2》,1240.3,2018);
f[4]=新电影(“寻找多莉”,1028.52016);
f[5]=新电影(《史莱克2》,919.82004);
f[6]=新电影(《丛林书》,966.52016);
f[7]=新电影(《卑鄙的我2》,970.71013);
int yearEntered=getInput.getUserInput(1991年、2018年);
System.out.printf(“\n您收到%s的银行”,输入年份);
电影总数=获得总收入(f,输入年份);
System.out.printf(“\n为%s制作的总金额为$%2.2f”,yearEntered,total.getFilmIncome());
Films top=getTopFilm(f,输入年份);
if(top==null){
系统输出打印F(“0”);
}否则{
System.out.printf(“\n在%s中,一部电影的最大收入是%s的$2.2f”,yearented,
top.getFilmTitle(),top.getFilmIncome());
}
}
私人静态电影getTotalIncome(电影[]f,输入年份){
总收入=零;
双加=0;
对于(int i=0;imax){
顶膜=f[i];
}
}
返回顶片;
}

您的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;
    }
private static Films getTopFilm(Films[]f,int yearented){
胶片topFilm=null;
双最大值=0;
对于(int i=0;imax){
顶膜=f[i];
max=f[i].getFilmIncome();//忘记设置max的值。
}
}
返回顶片;
}
公共静态void main(字符串[]args){
电影[]f=新电影[8];
f[0]=新电影(“冻结”,1290.012013);
f[1]=新电影(《狮子王》,968.41994);
f[2]=新电影(“Zootopia”,1023.7,2016);
f[3]=新电影(《不可思议的2》,1240.3,2018);
f[4]=新电影(“寻找多莉”,1028.52016);
f[5]=新电影(《史莱克2》,919.82004);
f[6]=新电影(《丛林书》,966.52016);
f[7]=新电影(《卑鄙的我2》,970.71013);
int yearEntered=getInput.getUserInput(1991年、2018年);
System.out.printf(“\n您收到%s的银行”,输入年份);
电影总数=获得总收入(f,输入年份);
System.out.printf(“\n为%s制作的总金额为$%2.2f”,yearEntered,total.getFilmIncome());
Films top=getTopFilm(f,输入年份,总计.getFilmIncome());
if(top==null){
系统输出打印F(“0”);
}否则{
System.out.printf(“\n在%s中,电影获得的最大收入是%s在$2.2f”,yearEntered,top.getFilmTitle(),top.getFilmIncome());
}
}
私人静态电影getTotalIncome(电影[]f,int yearented){
总收入=零;
双加=0;
双倍最大值=双倍最小值;
对于(inti=0;i
就像维韦克说的,你的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;
}
我不知道你为什么要从那里返回一个
电影
对象,你想做的是反复浏览电影,总结当年的所有电影

然后,您将编辑您的主函数以这样调用它

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

对于一个特定的年份,而你却进入了两年?@zlakad输入应该在1991年和2018年的范围内。我进入了2016年。因此,,“感谢您收到2016。您在
getTotalIncome
方法中有一些逻辑错误。因为它被编码为返回列表中的最后一张胶片,而您想要的值被计算并存储在变量
add
中。同样在
getTopFilm
方法中,变量
max
始终为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; 
}
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;
}
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;
}
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);
    ...
}