0

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

这是我的代码示例:

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];
        }
    }

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

4

3 回答 3

0

Just before your

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

write

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

and then print thenewmean

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

You don't need to write your

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

code anywhere. Even then if you wish to use your own code, then use it in your AirthmeticMean() function

于 2013-05-05T17:57:21.740 回答
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;
}
于 2013-05-06T03:03:00.480 回答
0

您获得的代码示例:

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

几乎可以。result几乎,因为您检索了在 for 循环中命名的数组的长度并访问了 array 的元素series

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

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

于 2013-05-05T17:51:36.483 回答