当我运行此程序时,阅读器仅读取并显示每隔一行。我究竟做错了什么?
while (imputFile.hasNext()) {
grade = imputFile.nextDouble();
System.out.println(grade);
if (grade < 0 || grade > 100)
System.out.print("Grade " + grade + " was invalid and ignored");
else {
numberOfGrades++;
sum += imputFile.nextDouble();
}
}
averageGrade = sum/numberOfGrades;
System.out.println("There were "+ numberOfGrades + " valid grades.");
System.out.printf("%3.2f",averageGrade);
最佳答案
您在循环中两次使用imputFile.nextDouble()
:
grade = imputFile.nextDouble();
//....
sum += imputFile.nextDouble();
尝试将最后一行更改为
sum += grade;
使用已经读取的值,而不是读取下一个值。
关于java - 在阅读试图确定有效成绩的文件时,为什么文件阅读器会跳过行?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19170950/