我知道这是计算数据数组模式的方法。
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;)。我该怎么做?
答案 0 :(得分:0)
为计数相等时添加条件:
if (count > maxCount) {
maxCount = count;
maxValue = a[i];
} else if (count == maxCount && maxValue != a[i]) {
maxValue = Double.NaN;
}
演示(在Javascript中):http://jsfiddle.net/Guffa/xWvAV/