在计算算术平均值时,如何丢弃最大和最小的数字?

时间:2013-05-05 17:42:28

标签: java math

我的问题是当我的程序计算算术平均值时丢弃最大和最小的数字。

这是我的代码示例:

public static void main(String[] args) {
    int k;
    int r;
    int thelargest;
    int thesmallest;

    Scanner input = new Scanner(System.in);
    System.out.println("Enter the list of number : ");
    String input2 = input.nextLine();

    String[] numbers = input2.split(" ");

    int[] result = new int[numbers.length];
    for (r = 0; r < numbers.length; r++) {
        result[r] = Integer.parseInt(numbers[r]);

    }

    for (k = 0; k < result.length; k++) {
        System.out.print("");
        System.out.println(result[k]);
    }

    System.out.println(" LargestNumber :  " + TheLargestNumber(result));
    System.out.println(" SmallestNumber :  " + TheSmallestNumber(result));
    thelargest = TheLargestNumber(result);
    thesmallest = TheSmallestNumber(result);
    System.out.println("The Arithmetic Mean : " + AirthmeticMean(result));

}

public static int TheSmallestNumber(int[] series) {
    int thesmallest = series[0];
    for (int i = 1; i < series.length; i++) {
        if (series[i] < thesmallest) {

            thesmallest = series[i];
        }
    }
    return thesmallest;
}

public static int TheLargestNumber(int[] series) {
    int thelargest = series[0];
    for (int i = 1; i < series.length; i++) {
        if (series[i] > thelargest) {

            thelargest = series[i];
        }
    }
    return thelargest;
}

public static float AirthmeticMean(int[] result) {
    int sum = 0;
    for (int i = 0; i < result.length; i++) {
        sum += result[i];
    }
    return (float) sum / result.length;
}

我试图找到方法并且我编写了这个示例,但我不知道如何嵌入此代码示例:

         for (int i = 0; i < result.length; i++) {
        if (series[i] != thesmallest && series[i] != thelargest) {
            total = total + seriess[i];
        }
    }

此代码示例对我有帮助吗?

3 个答案:

答案 0 :(得分:0)

您获得的代码示例:

for (int i = 0; i < result.length; i++) {
    if (series[i] != thesmallest && series[i] != thelargest) {
        total = total + seriess[i];
    }
}

几乎没问题。几乎是因为你在for-loop和数组result的访问元素中检索名为series的数组的长度。

您可以通过以下方式使用此代码。使用两个参数AirthmeticMeantheSmallest扩展theLargest,并跟踪求和元素的数量:

public static float AirthmeticMean(int[] result, int theSmallest, int theLargest) {
    int sum = 0;
    int numElements = 0;
    for (int i = 0; i < result.length; i++) {
        if (result[i] != theSmallest && result[i] != theLargest) {
            sum += result[i];
            numElements++;
        }
    }
    return (float) sum / numElements;
}

编辑:添加numElements

答案 1 :(得分:0)

在你的

之前
System.out.println("The Arithmetic Mean : " + AirthmeticMean(result));

thenewmean = (AirthmeticMean(result)*result.length - thesmallest - thelargest)/(result.length-2)

然后打印thenewmean

System.out.println("The Arithmetic Mean : " + thenewmean);

您无需编写

for (int i = 0; i < result.length; i++) {
    if (series[i] != thesmallest && series[i] != thelargest) {
        total = total + seriess[i];
    }
}

任何地方的代码。即使这样,如果你想使用自己的代码, 然后在AirthmeticMean()函数

中使用它

答案 2 :(得分:0)

如果删除最高和最小

,需要跟踪计数和总和
public static float AirthmeticMean(int[] result, int theSmallest, int theLargest) {
    int sum = 0;
    int cnt = 0;
    for (int i = 0; i < result.length; i++) {
            if (result[i] != theSmallest && result[i] != theLargest) {
                sum += result[i];
                cnt++;
            }
    }
    return (float) sum / cnt;
}