传递数组以获得java中的均值中位数和模式

时间:2016-03-24 02:22:37

标签: java arrays mean mode median

我正在尝试使用java传递数组来获取java中的均值,中位数,模式,最大值。我目前有一个问题将数组传递给函数并返回其值,以便我可以输出结果。我相信我有正确的循环来解决平均中位数和模式,但我不能让他们发送和接收所需。如何传递数组并发回所需的值?

更新:我已经更新了它将编译的代码,我可以输入年数,但之后我得到了几个错误。它也没有打印输出

线程“main”中的异常java.util.UnknownFormatConversionException:Conversion ='i'     at java.util.Formatter $ FormatSpecifier.conversion(Formatter.java:2646)     at java.util.Formatter $ FormatSpecifier。(Formatter.java:2675)     在java.util.Formatter.parse(Formatter.java:2528)     在java.util.Formatter.format(Formatter.java:2469)     在java.io.PrintStream.format(PrintStream.java:970)     在java.io.PrintStream.printf(PrintStream.java:871)     at la5cs1110_woodspl_03.pkg17.pkg2016.La5cs1110_WoodsPl_03172016.main(La5cs1110_WoodsPl_03172016.java:56) Java结果:1

    public static void main(String[] args) {
        int i;
        List<Double> hArray = new ArrayList<>();
        int nYears = 0, y = 0;
        double rMax = 0.00,rMin = 100.00;


        //get input check if between 1-80
        while(y == 0){
        String userData = JOptionPane.showInputDialog
                                   ("Enter number of years");
        nYears = Integer.parseInt(userData);

        if (nYears > 1 && nYears <= 80 )
           y = 1;

        }


        y = 0;
        while(y <= nYears){ 
            for(i = 0; i < 12; i++){
                Random rand = new Random();
                double rNum = rand.nextFloat() * (rMax - rMin) + rMin;
                 hArray.add(rNum);
            }
                double mean = getMean (hArray);
                double median = getMedian (hArray);
                double mode = getMode (hArray);
                double max = getMaxValue(hArray);
                double min = getMinValue (hArray);
           System.out.printf("In year %i the Mean = %d , mode = %d, median = %d," +
                  " max = %d, min = %d", y , mean, median, mode, max, min);
            y++;
        }

    }
    private static double getMean(List<Double> hArray) {
    double sum = 0;
    for (int i = 0; i < hArray.size(); i++) {
        sum += hArray.get(i);
    }
    return sum / hArray.size();
}
    //Median
    private static double getMedian(List<Double> hArray) {
    int middle = hArray.size()/2;
    if (hArray.size() %2 == 1) {
        return hArray.get(middle);
    } else {
        return (hArray.get(middle-1) + hArray.get(middle)) / 2.0;
    }
}
    //Mode
    public static double getMode(List<Double> hArray) {
    double maxValue = 0, maxCount = 0;

    for (int i = 0; i < hArray.size(); ++i) {
        int count = 0;
        for (int j = 0; j < hArray.size(); ++j) {
            if (hArray.get(j) == hArray.get(i)) ++count;
        }
        if (count > maxCount) {
            maxCount = count;
            maxValue = hArray.get(i);
        }
    }

    return maxValue;
}
    public static double getMaxValue(List<Double> hArray){  
      double maxValue = hArray.get(0);  
      for(int i=1;i < hArray.size();i++){  
      if(hArray.get(i) > maxValue){  
      maxValue = hArray.get(i);  

         }  

     }  
         return maxValue; 
    }
    public static double getMinValue(List<Double> hArray){  
     double minValue = hArray.get(0);  
     for(int i=1;i<hArray.size();i++){  
     if(hArray.get(i) < minValue){  
     minValue = hArray.get(i);  
        }  
     }  
    return minValue;  
 }  


}

3 个答案:

答案 0 :(得分:1)

您的hArray列表。您应该首先将其转换为数组。

getMean(hArray.toArray)

查看this

答案 1 :(得分:1)

使用整个数组调用替换您尝试将单个元素从数组传递到统计函数的部分并更改调用的签名,以便它们采用List<Double>参数,而不是一个double[]。像这样:

   double mean = getMean (hArray);
   double median = getMedian (hArray);
   double mode = getMode (hArray);
   double max = getMaxValue(hArray);
   double min = getMinValue (hArray);

//Mean
private static double getMean(List<Double> hArray) {
    double sum = 0;
    for (int i = 0; i < hArray.size(); i++) {
        sum += hArray.get(i);
    }
    return sum / hArray.size();
}

另请参阅:How do you calculate the variance, median, and standard deviation in C++ or Java?

修正中位数:

直接从上面的链接复制并进行一些小修改,以使用List作为参数

public Double median(List<Double> list) 
{
   Double[] array = list.toArray(new Double[list.size()]);
   Arrays.sort(data);

   if (data.length % 2 == 0) 
   {
      return (data[(data.length / 2) - 1] + data[data.length / 2]) / 2.0;
   } 
   else 
   {
      return data[data.length / 2];
   }
}

修复模式:

public Double mode(List<Double> list) 
{
    java.util.TreeMap<Double,Integer> map = new java.util.TreeMap<>();
    Double maxVal = null;
    int maxCount = 0;

    for (Double d : list) {
        int count = 0;
        if (map.containsKey(d)) {
            count = map.get(d) + 1;
        } else {
            count = 1;
        }
        map.put(d, count);

        if (count > maxCount) {
           maxVal = d;
           maxCount = count;
        }
    }
    return maxVal;
}

答案 2 :(得分:1)

这不会编译,您尝试将Double传递给方法,该方法需要double[]。因此,您必须更改方法的参数并使用List并传入hArray(请参阅Tibrogargan答案 - 即,您必须修改每个实现)或执行以下操作:

  1. 创建Double[]

    Double[] hArray2 = hArray.toArray(new Double[hArray.size()]);
    
  2. 更改您的方法&#39;签名,以便他们期望Double[]

    private static double getMean(Double[] hArray) { ...}
    
  3. 传递hArray2而不是hArray

    double mean = getMean(hArray2);
    // ...
    
  4. 应该是它。