如何找到此数组的最低和最高值(java)

时间:2016-12-16 15:01:16

标签: java

这是我目前的代码无法让它工作。无法打印出正确的值。目前,最高打印为2897,打印最低为2897。

public static void main(String[] args) {

    int [] salesfigures2014 = {2200,1951,2181,2888,3084,2897};
    int [] salesfigures2015 = {2359,2099,2352,2952,3274,3264};
    String [] salesfiguresmonths = {"Jan","Feb","March","April","May","June"};  

    int Average2014 = avgsales2014(salesfigures2014);
    int Highest2014 = highmonth2014(salesfigures2014,salesfiguresmonths);
    int Lowest2014 = lowmonth2014(salesfigures2014,salesfiguresmonths);
    //int Average2015 = avgsales2015(salesfigures2015);
    //int Highest2015 = highmonth2015(salesfigures2015,salesfiguresmonths);
    //int Lowest2015 = lowmonth2015(salesfigures2015,salesfiguresmonths);
    //int AverageSales = avgmonth(salesfigures2014,salesfigures2015,salesfiguresmonths);

    System.out.println("highest sales in 2014: " + highmonth2014(salesfigures2014, salesfiguresmonths));
    System.out.println("lowest sales in 2014: " + lowmonth2014(salesfigures2015, salesfiguresmonths));


}

public static int avgsales2014(int[] salesfigures2014) {

    int i, total = 0; 
    for(i=0; i<salesfigures2014.length; i++)            
    {
        total = total + salesfigures2014[i];     

    }
    total = total/salesfigures2014.length;
    return(total);

}
static int highmonth2014(int[] salesfigures2014, String[] salesfiguresmonths) {

    int high = salesfigures2014[0];
    for (int i = 1; i < salesfigures2014.length; i++){
        if(salesfigures2014[i] > high );
        high = salesfigures2014[i];
    }
    return high;
}
static int lowmonth2014(int[] salesfigures2014, String[] salesfiguresmonths) {

    int low = salesfigures2014[0];
        for (int i = 1; i < salesfigures2014.length; i++){
            if(salesfigures2014[i] < low );
            low = salesfigures2014[i];
        }
        return low;
}

1 个答案:

答案 0 :(得分:3)

摆脱那些额外的(和错误的); s。

enter image description here