我必须实现一个名为“
computeMeanRoot
”。该方法获取一个双精度数组和两个整数值作为输入参数,并返回一个双精度值。签名:computeMeanRoot(double [] array,int startindex,int endindex):double该方法从指定范围内(从startIndex到endIndex)的数组中的数字计算平均值。根据平均值计算平方根并返回。
两个指标的负值均应忽略
有更好的方法吗?
public static double [] berechneMittelwertWurzel(int startindex, int endindex) {
double arr[] = { 5.2, 66.23, -4.2, 0.0, 53.0 };
double sum = 0;
for(int i=0; i<arr.length; i++){
sum = sum + arr[i];
double average = sum / arr.length;
for(int i =0; i < arr.length;i++)
{
for(int j = 0;j < arr.length;j++)
{
if(Math.sqrt(arr[i]) == arr[j])
{
s += arr[j] + "," + arr[i] + " ";
if(index < 0 || index >= array.length)
throw new IndexOutOfBoundsException();
您发布的功能不符合练习中的规范。存在各种问题:
另外,您的问题标题提到中位数;但是,您正在尝试计算算术平均值。从问题文本来看,这实际上可能是正确的。请注意,这两个值通常是不同的。
要解决此问题,请从以下签名开始并填补空白:
public static double calculateMeanRoot(double[] array, int startindex, int endindex) {
// if the startIndex > endIndex is an IntervalException should be thrown
// if the endIndex > array.length -1 or startIndex < 0, the IndexOutOfBoundsException shall be thrown
// calculate the sum of the array elements between `startIndex` and `endIndex` (*)
// calculate the mean by dividing the sum by the difference between `endIndex` and `startIndex`
// if the calculated mean is less than zero (<0), then a NegativeNumberException shall be thrown with an output
// calculate the square root of the mean, round it, and return it
}
仅带有注释的步骤(*)
需要循环。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句