我知道这就是计算数据数组的模式的方法。
public double mode() {
int maxValue=0, maxCount=0;
for (int i = 0; i < a.length; i++) {
int count = 0;
for (int j = 0; j < a.length; j++) {
if (a[j] == a[i])
++count;
}
}
if (count > maxCount) {
maxCount = count;
maxValue = a[i];
}
}
return maxValue; }当有多个值可以是模式时,我遇到了一个问题。所以如果有多个值是一个模式,我想输出(返回Double.NaN;)。我该怎么做?
发布于 2013-10-28 06:40:14
添加计数相等时的条件:
if (count > maxCount) {
maxCount = count;
maxValue = a[i];
} else if (count == maxCount && maxValue != a[i]) {
maxValue = Double.NaN;
}演示(使用Javascript):http://jsfiddle.net/Guffa/xWvAV/
https://stackoverflow.com/questions/19624430
复制相似问题