我的任务是创建一个程序来对一班学生的分数进行统计分析。班级最多可容纳 40 名学生。学期内有五次测验。每个学生都有一个四位数的学生证号。该程序是打印学生的分数,并计算并打印每个测验的统计数据。我们打算使用下面提供的 4 个不同的类
package lab2;
import java.util.Arrays;
public class Student {
private int SID;
private int[] scores = new int[5];
public int getSID() {
return SID;
}
public void setSID(int SID) {
this.SID = SID;
}
public int[] getScores() {
return scores;
}
public void setScores(int[] scores) {
this.scores = scores;
}
public void print() {
System.out.println(SID + " " + Arrays.toString(scores));
}
}
package lab2;
import java.util.Arrays;
public class Statistics {
private int [] lowscores = new int [5];
private int [] highscores = new int [5];
private float [] avgscores = new float [5];
void findlow(Student [] a){
for (int i = 0; i < lowscores.length; ++i) {
lowscores[i] = 100;
for (Student student : a) {
if (student.getScores()[i] < lowscores[i]) {
lowscores[i] = student.getScores()[i];
}
}
}
}
void findhigh(Student [] a){
for (int i = 0; i < highscores.length; ++i) {
highscores[i] = 0;
for (Student student : a) {
if (student.getScores()[i] > highscores[i]) {
highscores[i] = student.getScores()[i];
}
}
}
}
void findavg(Student [] a) {
for (int i = 0; i < avgscores.length; ++i) {
avgscores[i] = 0;
for (Student student : a) {
avgscores[i] += student.getScores()[i];
}
avgscores[i] /= a.length;
}
}
public void print() {
System.out.println("High Score " + Arrays.toString(highscores));
System.out.println("Low Score " + Arrays.toString(lowscores));
System.out.println("Average " + Arrays.toString(avgscores));
}
}
package lab2;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Util {
public static Student[] readFile(String StudentScores, Student[] stu) {
int i = 0;
//Reads the file and builds student array.
try { FileReader File = new FileReader(StudentScores);
BufferedReader buff = new BufferedReader(File);
// skipping header row
boolean eof = buff.readLine() == null;
while (!eof) {
String line = buff.readLine();
if (line == null)
eof = true;
else {
String[] data = line.split(" ");
stu[i] = new Student();
stu[i].setSID(Integer.parseInt(data[0]));
int[] scores = new int[data.length - 1];
for (int j = 1; j < data.length; ++j) {
scores[j - 1] = Integer.parseInt(data[j]);
}
stu[i].setScores(scores);
++i;
}
}
buff.close();
} catch (IOException e) {
System.out.println("Error -- " + e.toString());
}
Student[] students = new Student[i];
System.arraycopy(stu, 0, students, 0, i);
return students;
}
}
package lab2;
import java.io.FileNotFoundException;
public class Driver {
public static void main(String[] args) throws FileNotFoundException {
Student[] lab2 = new Student[40];
// Populate the student array
lab2 = Util.readFile("filename.txt", lab2);
Statistics statlab2 = new Statistics();
statlab2.findlow(lab2);
statlab2.findhigh(lab2);
statlab2.findavg(lab2);
System.out.println("Stud Qu1 Qu2 Qu3 Qu4 Qu5");
for (Student student : lab2) {
student.print();
}
statlab2.print();
}
}
输入应该从文本文件中读取。我有一个名为
StudentScores.txt
的文件,但是当我尝试运行它时,我收到了这条消息。我不确定从这里去哪里或如何解决这个问题。一些帮助将不胜感激。
Error -- java.io.FileNotFoundException: StudentScores.txt (The system cannot find the file specified)
Stud Qu1 Qu2 Qu3 Qu4 Qu5
High Score [0, 0, 0, 0, 0]
Low Score [100, 100, 100, 100, 100]
Average [NaN, NaN, NaN, NaN, NaN]
`