我想寫一個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);
}
}
感謝您的時間!
注意,這個問題沒有得到很好的定義。在任何階段,如果最小值和最大值與平均值等距,那麼刪除哪個值?您的選擇將影響所有後續平均值。 –