给出该题的Java代码:学生成绩存储在文件中,每行一个学生成绩,形如: 20151201 85 84 91.5 20151202 70 85 65 … 请编程读入学生成绩,并计算各门课程的均值和方差,写入文件的最后一行,如: 均值和方差 71/10 81.2/11.3 76/15.3
时间: 2024-02-05 08:13:50 浏览: 21
以下是该题的Java代码:
```java
import java.io.*;
public class ScoreAnalysis {
public static void main(String[] args) {
// 定义变量
int count = 0;
double sum1 = 0, sum2 = 0, sum3 = 0;
double avg1 = 0, avg2 = 0, avg3 = 0;
double var1 = 0, var2 = 0, var3 = 0;
// 读入学生成绩
try {
BufferedReader br = new BufferedReader(new FileReader("scores.txt"));
String line = "";
while ((line = br.readLine()) != null) {
String[] fields = line.split(" ");
double score1 = Double.parseDouble(fields[1]);
double score2 = Double.parseDouble(fields[2]);
double score3 = Double.parseDouble(fields[3]);
sum1 += score1;
sum2 += score2;
sum3 += score3;
count++;
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
// 计算均值和方差
avg1 = sum1 / count;
avg2 = sum2 / count;
avg3 = sum3 / count;
try {
BufferedReader br = new BufferedReader(new FileReader("scores.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("scores.txt", true));
String line = "";
while ((line = br.readLine()) != null) {
bw.write(line + "\n");
}
var1 = calcVariance("scores.txt", 1, avg1);
var2 = calcVariance("scores.txt", 2, avg2);
var3 = calcVariance("scores.txt", 3, avg3);
bw.write(String.format("均值和方差 %.1f/%.1f %.1f/%.1f %.1f/%.1f", avg1, var1, avg2, var2, avg3, var3));
br.close();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 计算方差
public static double calcVariance(String filename, int col, double mean) throws IOException {
double variance = 0;
int count = 0;
BufferedReader br = new BufferedReader(new FileReader(filename));
String line = "";
while ((line = br.readLine()) != null) {
String[] fields = line.split(" ");
double score = Double.parseDouble(fields[col]);
variance += (score - mean) * (score - mean);
count++;
}
br.close();
variance /= count;
return variance;
}
}
```
该程序首先读入文件中的学生成绩,并计算各门课程的均值。然后再次读入文件,计算各门课程的方差,并将结果写入文件的最后一行。其中,`calcVariance` 方法用于计算方差,它接受三个参数:文件名、列号和均值。
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)