私は、テキストファイルからデータを読み込み、与えられた質問に従っていくつかの異なる平均を計算するJavaプログラムを作成しようとしています。ループを使用して平均値から最も離れた値を見つけるにはどうすればよいですか?
- すべての値の平均:は
私は与えられていたファイルは、私が見つけるためにしようとしている(ファイルの1行目に示されている)13の異なる進数
が含まれています。
- A1から最も遠いものを除く、すべての値の平均値。
- A2から最も離れた2つを除く、すべての値の平均値。
- A3から最も遠い3つを除くすべての値の平均値。
- 。 。 。
- A(N-1)から最も離れたN-1を除く、すべての値の平均。 (これはただ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);
}
}
感謝を!
注:ここでは
は高速なソリューションのためのコードです。どの段階でも、最小値と最大値が平均値から等距離にある場合は、どれを削除しますか?あなたの選択は、その後のすべての平均に影響します。 –