我正在做的程序要求我使用 FileInputStream 来计算课程的成绩。
我创建的类 Course 有一个方法:
public double getGradePoint ()
{
if (letterGrade == "A+" || letterGrade == "A")
gradePoint = 4.0;
if (letterGrade == "A-")
gradePoint = 3.7;
if (letterGrade == "B+")
gradePoint = 3.3;
if (letterGrade == "B")
gradePoint = 3.0;
if (letterGrade == "B-")
gradePoint = 2.7;
if (letterGrade == "C+")
gradePoint = 2.3;
if (letterGrade == "C")
gradePoint = 2.0;
if (letterGrade == "C-")
gradePoint = 1.7;
if (letterGrade == "D+")
gradePoint = 1.5;
if (letterGrade == "D")
gradePoint = 1.0;
if (letterGrade == "F")
gradePoint = 0.0;
return gradePoint;
}
在测试客户端中,我有:
String subject;
String number;
String letGrade;
Course course1 = new Course ("ENGL", "1111", "D+");
FileInputStream file1 = new FileInputStream ("test transcript 1.txt");
Scanner readFile = new Scanner (file1);
while (readFile.hasNext ())
{
course1.setSubject (readFile.next ());
subject = course1.getSubject ();
course1.setNumber (readFile.next ());
number = course1.getNumber ();
course1.setLetterGrade (readFile.next ());
letGrade = course1.getLetterGrade ();
course1.getGradePoint ();
System.out.println (course1.toString ());
}
测试成绩单 1.txt 包含:
COMP 1631 A-
ENGL 1101 B
MATH 1200 C+
但是由于某种原因,当我使用 course1.getGradePoint() 时,它总是返回为 0.0
运行程序后:
COMP 1631 A- (worth = 0.0)
ENGL 1101 B (worth = 0.0)
MATH 1200 C+ (worth = 0.0)
我尝试在 while 命令上方使用 course1.getGradePoint () ,如下所示:
String subject;
String number;
String letGrade;
Course course1 = new Course ("ENGL", "1111", "D+");
course1.getGradePoint ();
FileInputStream file1 = new FileInputStream ("test transcript 1.txt");
Scanner readFile = new Scanner (file1);
while (readFile.hasNext ())
{
course1.setSubject (readFile.next ());
subject = course1.getSubject ();
course1.setNumber (readFile.next ());
number = course1.getNumber ();
course1.setLetterGrade (readFile.next ());
letGrade = course1.getLetterGrade ();
course1.getGradePoint ();
System.out.println (course1.toString ());
}
返回为:
COMP 1631 A- (worth = 1.5)
ENGL 1101 B (worth = 1.5)
MATH 1200 C+ (worth = 1.5)
由于某种原因,它不能与 readFile.next () 一起使用。如果有人可以帮助我,那将不胜感激。