2016-12-16 125 views
-1

這是我當前的代碼無法使其工作。不會打印出正確的值。目前,打印出最高爲2897,並打印出最低爲2897.如何查找此數組的最低和最高值(java)

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; 
} 
+8

你需要你的if語句後刪除裏面你還for循環 – CraigR8806

+0

,你應該是縮進設置的高和低,爲了提高可讀性行分號。 – CraigR8806

+2

我會建議簡單地使用['IntSummaryStatistics'](https://docs.oracle.com/javase/8/docs/api/java/util/IntSummaryStatistics.html)。 –

回答

相關問題