在这个作业中,我们假设从 txt 文件中读取测试和家庭作业的分数并计算最终成绩。由于我们还没有学习读写文件,所以这是给我们的预制方法:
/******************************************************************************
*
* Filename : GradeCalculatorFromFile.java.
* Author: xxxxxxxxx
* Date: 09/025/2011
* Description: This program computes the scores of a list of students in the CSE155a class
*
******************************************************************************/
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
/* Provide a description of the class */
public class GradeCalculatorFromFile {
public static void main(String[] args) {
// Declare and initialise the variables as needed
int anthonyHopkinsTest = 0;
int anthonyHopkinsHomework = 0;
/*
* The following code enables the user to accept input from the
* keyboard. Keep this code as it is.
*/
Scanner scanner = null;
try {
scanner = new Scanner(new File("grades.txt"));
} catch (FileNotFoundException e) {
System.out.println("Error opening file. Please make sure that you have a grades.txt file in the same folder as GradeCalculator.class");
System.exit(0);
}
/*
* Add your code here to read the number of students and the scores Use
* scanner.next() to read a String Use scanner.nextInt() to read an int
*/
}
}
这是我们假设使用的 txt 文件:
4
Anthony Hopkins 80 90 95 87 80 78 25 17 20 22 21 24 19 22 21 23 24 21 20 25 20 55 56 110 30 20 25 8
John Smith 99 95 82 72 64 52 15 14 11 21 25 12 19 20 21 23 21 12 12 10 15 50 50 60 25 15 20 9
Pan Mei 85 92 72 45 82 78 22 13 16 22 24 10 18 12 21 24 25 10 11 14 20 58 51 95 28 14 28 7
Rafael Vega 99 45 87 52 87 99 25 25 21 21 14 19 19 25 25 20 20 18 20 24 20 60 60 60 25 16 23 8
问题是我们没有被教导如何从文件中获取信息。说明说要使用扫描仪来获得成绩,但我不知道该怎么做。一组中的前21个数字是家庭作业,接下来的3个是测试,最后4个是家庭作业。我知道如何使用用户输入来做这种事情,但在从文件中读取信息时不知道