我正在编写一个程序,该程序将从一个充满学生成绩的文本文件中获取输入。这些成绩被平均并添加到数组列表中。但是,我无法读取要读取的文件。我收到输入不匹配。我尝试将我的 calcAssignmentAverage 方法中的循环更改为小于 15(有 15 个实验室成绩,文本文件中的第二行),但它仍然给出输入不匹配。
文件(真实文件有16名学生,格式如下。):
Puckett, Karen
10 10 9.5 10 10 8 9.5 10 10 10 9 10 10 10 0
4 3 5 3 5 2 3 2 1.5 1 5 3.5
17.5 24 22 23.5 22 23
90 91
96
代码:
public static void main(String[] args) throws IOException {
final double NUM_OF_LABS = 15;
final double NUM_OF_QUIZZES = 12;
final double NUM_OF_PROJECTS = 6;
final double NUM_OF_EXAMS = 2;
final double NUM_OF_FINAL = 1;
final double LAB_POINTS = 150;
final double QUIZ_POINTS = 60;
final double PROJECT_POINTS = 150;
final double EXAM_POINTS = 200;
final double FINAL_POINTS = 100;
//Open input file
File myFile = new File("scores.txt");
if (!myFile.exists()) {
System.out.println("The input file was not found.");
System.exit(0);
}
Scanner inputFile = new Scanner(myFile);
ArrayList<Student> studentList = new ArrayList<Student>();
readFile(inputFile, studentList, NUM_OF_LABS, LAB_POINTS, NUM_OF_QUIZZES,
QUIZ_POINTS, NUM_OF_PROJECTS, PROJECT_POINTS, NUM_OF_EXAMS,
EXAM_POINTS, NUM_OF_FINAL, FINAL_POINTS);
}
public static void readFile(Scanner inputFile,
ArrayList<Student> studentList, double NUM_OF_LABS,
double LAB_POINTS, double NUM_OF_QUIZZES, double QUIZ_POINTS,
double NUM_OF_PROJECTS, double PROJECT_POINTS,
double NUM_OF_EXAMS, double EXAM_POINTS, double NUM_OF_FINAL,
double FINAL_POINTS) {
//read and set data for student1:
while (inputFile.hasNext()) {
//create a new student
Student currentStudent = new Student();
//read name
String name;
name = inputFile.nextLine();
currentStudent.setName(name);
//read lab average
double labAverage = 0;
labAverage = calcAssignmentAverage(NUM_OF_LABS, inputFile, LAB_POINTS);
currentStudent.setLabAverage(labAverage);
//add currentStudent to the list
studentList.add(currentStudent);
}
}
public static double calcAssignmentAverage(double NUM_ASSIGNMENTS,
Scanner inputFile, double MAX_POINTS) {
double sum = 0;
double average = 0;
for (int i = 0; i < NUM_ASSIGNMENTS; i++) {
sum = sum + inputFile.nextDouble();
}
average = (sum / MAX_POINTS) * 100;
return average;
}