简体   繁体   中英

How to I find lowest and highest value of this array (java)

Here is my current code cannot get it to work. Will not print out correct values. Currently, prints out Highest as 2897 and also prints out lowest as 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;
}

Get rid of those extra (and wrong) ; s.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM