我有一个分离的 .txt 文件,其中包含一个“学生”列表和他们的分数(从 0 到 10),以下是 .txt 文件的示例:
Mark 2
Elen 3
Luke 7
Elen 9
Jhon 5
Mark 4
Elen 10
Luke 1
Jhon 1
Jhon 7
Elen 5
Mark 3
Mark 7
我想要做的是计算每个学生的平均分(以 double
形式表示),以便输出如下:
Mark: 4.0
Elen: 6.75
Luke: 4.0
Jhon: 4.33
这是我现在想到的,目前我只能使用 Properties
列出学生姓名而不重复,但每个人旁边显示的数字显然是程序找到的最后一个。
我已将它包含在按钮动作侦听器中,因为我正在实现 GUI,在按下按钮时,上面显示的输出会在 TextArea
中 append
:
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent d) {
try {
File file = new File("RegistroVoti.txt");
FileInputStream fileInput = new FileInputStream(file);
Properties properties = new Properties();
properties.load(fileInput);
fileInput.close();
Enumeration enuKeys = properties.keys();
while (enuKeys.hasMoreElements()) {
String key = (String) enuKeys.nextElement();
String value = properties.getProperty(key);
l1.append(key + ": " + value + "\n");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
我在考虑使用
Collectors
来计算平均值,但实际上我不知道如何实现它...非常感谢您的帮助!
提前致谢!