java - 如何使用循环找到距离平均值最远的值?

标签 java loops

我正在尝试编写一个java程序,它从文本文件中读取数据并根据给定的问题计算一些不同的平均值。

我得到的文件包含 13 个不同的十进制数字(在文件的第一行中指示)

我正在寻找:

  • 所有值的平均值。 (将此平均值称为 A1。)
  • 所有值的平均值,不包括距 A1 最远的值。 (将此平均值称为 A2。)
  • 所有值的平均值,不包括距 A2 最远的两个值。 (将此平均值称为 A3。)
  • 所有值的平均值,不包括距 A3 最远的三个值。
  • 。 。 .
  • 所有值的平均值,不包括距 A(N-1) 最远的 N-1 个值。 (这是一个元素的平均值;换句话说,就是该元素本身的值。)

这是我的代码。我设法得到了 A1、A2、A3 和 A4,但我不知道下一步该做什么。 (我觉得我应该使用循环,但我不知道如何)

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class FurtherTweeking
{ public static void main ( String[] args ) throws IOException {

//Read the given file
Scanner scan = new Scanner(new File("C:\\Users\\IdeaProjects\\src\\ArrayList.txt"));
//The first line of the file gives the number of values that follow
int Num = scan.nextInt();

//Reads the data into an array
Double[] InputData = new Double[Num];
double ArraySum = 0;
int i = 0;
do {
    InputData[i] = scan.nextDouble();
    i = i+1;
}
while(scan.hasNextLine());
scan.close();






//Calculate the sum of all input data
for (int j = 0; j < Num; j++ ) {
    if (InputData[j] != null) {
        ArraySum = ArraySum + InputData[j];
    }
}
//Calculate the average of the original input data
double A1 = ArraySum/(Num);
System.out.println("A1: " + A1);





//Scan through the array to find the value that is farthest (in either direction) from the average
double Farthest = InputData[0];
for (int j = 0; j < Num; j++ ) {
    if (InputData[j] != null) {
        if ( Math.abs ( A1 - InputData[j] ) > Math.abs ( A1 - Farthest ) )
            Farthest = InputData[j];
    }
}
for (int u = 0; u < Num; u++){
    if (InputData[u] == Farthest ){
        InputData[u] = null;
    }
}
System.out.println("Most distant value: " + Farthest);
//compute an average that does not include the most distant value. Print the new average.
double A2 = ( ArraySum - Farthest )/( Num - 1.0 );
System.out.println("A2: " + A2 );






double Farthest2 = InputData[0];
double Farthest3 = InputData[0];
for (int j = 0; j < Num; j++ ) {
    if (InputData[j] != null) {
        if ( Math.abs ( A2 - InputData[j] ) > Math.abs ( A2 - Farthest2 ) ) {
            Farthest3 = Farthest2;
            Farthest2 = InputData[j];
        }
        else if ( Math.abs ( A2 - InputData[j] ) > Math.abs ( A2 - Farthest3 ) ) {
            Farthest3 = InputData[j];
        }
    }
}
System.out.println("Most distant value: " + Farthest2 + ", " + Farthest3);
//compute an average that does not include the most distant value. Print the new average.
double A3 = ( ArraySum - Farthest - Farthest2 - Farthest3 )/( Num - 3.0 );
System.out.println("A3: " + A3 );





double Farthest4 = InputData[0];
double Farthest5 = InputData[0];
double Farthest6 = InputData[0];
for (int j = 0; j < Num; j++ ) {
    if (InputData[j] != null) {
        if ( Math.abs ( A3 - InputData[j] ) > Math.abs ( A3 - Farthest4 ) ) {
            Farthest6 = Farthest5;
            Farthest5 = Farthest4;
            Farthest4 = InputData[j];
        }
        else if ( Math.abs ( A3 - InputData[j] ) > Math.abs ( A3 - Farthest5 ) ) {
            Farthest6 = Farthest5;
            Farthest5 = InputData[j];
        }
        else if ( Math.abs ( A3 - InputData[j] ) > Math.abs ( A3 - Farthest6 ) ) {
            Farthest6 = InputData[j];
        }
    }
}
System.out.println("Most distant value: " + Farthest4 + ", " + Farthest5+ ", " + Farthest6);
//compute an average that does not include the most distant value. Print the new average.
double A4 = ( ArraySum - Farthest - Farthest2 - Farthest3 -Farthest4 - Farthest5 - Farthest6 )/( Num - 6.0 );
System.out.println("A4: " + A4 );
}
}

感谢您的宝贵时间!!!

最佳答案

我将创建两个 util 函数:

/** Returns the average of a collection of double */
private static Double average(Collection<Double> coll) {
  return coll.stream().collect(Collectors.averagingDouble(Double::doubleValue));
}

/** Returns the first, most distant element of a collection from a defined value. */
private static Double furthest(Collection<Double> coll, Double value) {
  return coll.stream()
    .max((d1, d2) -> Double.compare(Math.abs(d1-value), Math.abs(d2-value)))
    .orElse(null);
}

像这样使用它们:

Double[] array = new Double[]{1d,2d,3d,4d,5d,6d,7d,8d,9d};
List<Double> list = new ArrayList<>(Arrays.asList(array));

for (int i = 0; i < array.length; i++) {

  double average = average(list);
  System.out.printf("Average A%d: %.1f, List: %s\n", (i+1), average, input);

  double furthest = furthest(list, average);
  list.remove(furthest);
}

输出:

Average A1: 5.0, List: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
Average A2: 5.5, List: [2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
Average A3: 6.0, List: [3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
Average A4: 6.5, List: [4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
Average A5: 7.0, List: [5.0, 6.0, 7.0, 8.0, 9.0]
Average A6: 7.5, List: [6.0, 7.0, 8.0, 9.0]
Average A7: 8.0, List: [7.0, 8.0, 9.0]
Average A8: 8.5, List: [8.0, 9.0]
Average A9: 9.0, List: [9.0]

关于java - 如何使用循环找到距离平均值最远的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43761579/

相关文章:

java - 使用 Hibernate 在外键字段中插入空值

javascript - 如何使用 for 循环删除 div 并延迟 5 秒

php - 循环遍历 MySQL 结果并将值保存到 $_SESSION

java - Java中服务器如何向客户端发送消息

java - LinearLayout 和 NestedScrollView 中的 RecyclerView - 如何滚动到某个位置的项目顶部?

java - 带条件替换的正则表达式

java - 排除数组的索引 0 作为输出选项?

c - 硬件启发循环。废话?

python - 如何使用 groupby 来避免 python 中的循环

java - 如何使用 ArrayList(在 Java 中)为 1x1 矩阵编写默认构造函数?