[英]How to I find lowest and highest value of this array (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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.