0
テキストファイル内のすべての行について、同じ行の数値の平均を求めようとしています。これはテキストファイルであれば はたとえば、:テキストファイルの平均値を求める
For Competitor #1, the average is 5.8625
For Competitor #2, the average is 0.0000
For Competitor #3, the average is 1.0000
これは私のコードです:
8.7 6.5 0.1 3.2 5.7 9.9 8.3 6.5 6.5 1.5
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
私のようなものを印刷したいです。
import java.io.*;
import java.util.*;
import java.text.*;
public class BaseClass
{
public static void main(String args[]) throws IOException
{
NumberFormat fmt = NumberFormat.getNumberInstance();
fmt.setMinimumFractionDigits(4);
fmt.setMaximumFractionDigits(4);
Scanner sf = new Scanner(new File("C:\\temp_Name\\DataGym.in.txt"));
int maxIndx = -1;
String text[] = new String[1000];
while (sf.hasNext()) {
maxIndx++;
text[maxIndx] = sf.nextLine();
}
sf.close();
int contestant = 0;
for (int j = 0; j <= maxIndx; j++) {
Scanner sc = new Scanner(text[j]);
double scoreAverage = 0;
double a = 0;
double array[] = new double[1000];
contestant++;
if (j <= 10) {
a += sc.nextDouble();
array[j] += a;
} else {
Arrays.sort(array);
int i = 0;
while (i < 10) {
scoreAverage += array[i];
i++;
}
}
String s = fmt.format(scoreAverage);
double d = Double.parseDouble(s);
System.out.println("For Competitor #" + contestant + ", the average is " + d);
}
}
}
それはあなたがここで問題を複雑に超えている
For the Competitor #1, the average is 0.0
For the Competitor #2, the average is 0.0
For the Competitor #3, the average is 0.0
あなたはあなたのコードをデバッグしたり、解析を投稿する必要があるが、これはあまりにも広範に答える(そして多分あなたの宿題をするための質問であります?) –