【问题标题】:Mean, Median, and Mode - Newb - Java均值、中位数和众数 - Newb - Java
【发布时间】:2015-05-20 05:13:52
【问题描述】:

我们在 Comsci 有一个我不知道的实验室。我在这个网站和其他网站上做了很多研究以寻求帮助,但他们超出了我的想象。让我失望的是阵列。无论如何,提前谢谢。我已经拿到了成绩,只是想知道如何做到这一点:D

PS:我的意思是,我只是找不到偶数中位数,我就放弃了。

import java.util.Arrays;
import java.util.Random;


public class TextLab06st
{

public static void main(String args[])
{
    System.out.println("\nTextLab06\n");
    System.out.print("Enter the quantity of random numbers  ===>>  ");
    int listSize = Expo.enterInt();
    System.out.println();
    Statistics intList = new Statistics(listSize);
    intList.randomize();
    intList.computeMean();
    intList.computeMedian();
    intList.computeMode();
    intList.displayStats();
    System.out.println();
}
}


class Statistics
{

private int list[];         // the actual array of integers
private int size;           // user-entered number of integers in the array
private double mean;        
private double median;      
private int mode;           

public Statistics(int s)
{
    size = s;
    list = new int[size];
    mean = median = mode = 0;
}

public void randomize()
{
    Random rand = new Random(12345);
    for (int k = 0; k < size; k++)
        list[k] = rand.nextInt(31) + 1;  // range of 1..31
}

public void computeMean()
{
    double total=0;
    for (int f = 0; f < size; f++)
    {
        total = total + list[f];
    }
    mean = total / size;

}

public void computeMedian()
{
    int total2 = 0;
    Arrays.sort(list);
    if (size / 2 == 1)
    {
     //   total2 =  
    } 
    else
    {
        total2 = size / 2;
        median = list[total2];
    }



}

public void computeMode()
{
    // precondition: The list array has exactly 1 mode.


}

public void displayStats()
{
    System.out.println(Arrays.toString(list));
    System.out.println();
    System.out.println("Mean:    " + mean);
    System.out.println("Median:  " + median);
    System.out.println("Mode:    " + mode);
}

}

【问题讨论】:

  • 到底是什么问题?
  • 我认为他希望我们帮助他完成他的代码。我喜欢这些问题,所以我不介意^^。
  • @Shahzeb 对不起,是的。我的问题是找出如何完成 computeMean 和 computeMode 方法
  • 为什么这个投票被否决了这么多?

标签: java arrays median


【解决方案1】:

以下是您的 median()mode() 方法的两个实现:

public void computeMedian() {
    Arrays.sort(list);
    if ( (list.size & 1) == 0 ) {
        // even: take the average of the two middle elements
        median = (list[(size/2)-1] + list[(size/2)]) / 2;
    } else {
        // odd: take the middle element
        median = list[size/2];
    }
}

public void computeMode() {
    // precondition: The list array has exactly 1 mode.
    Map<Integer, Integer> values = new HashMap<Integer, Integer>();
    for (int i=0; i < list.size; ++i) {
        if (values.get(list[i]) == null) {
            values.put(list[i], 1);
        } else {
            values.put(list[i], values.get(list[i])+1);
        }
    }

    int greatestTotal = 0;

    // iterate over the Map and find element with greatest occurrence
    Iterator it = values.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pair = (Map.Entry)it.next();
        if (pair.getValue() > greatestTotal) {
            mode = pair.getKey();
            greatestTotal = pair.getValue();
        }
        it.remove();
    }
}

【讨论】:

  • 我认为您在计算中位数之前错过了Array.sort(list) 电话
  • @TimBiegeleisen 感谢您撰写本文。有点过头了,但谢谢!
  • @TimBiegeleisen 我想我做到了,谢谢!抱歉,我是新来的。
猜你喜欢
  • 1970-01-01
  • 2017-01-11
  • 1970-01-01
  • 2019-03-07
  • 2021-04-14
  • 2021-05-12
  • 2017-12-16
  • 1970-01-01
  • 2021-11-10
相关资源
最近更新 更多