当我运行它时,阅读器只读取并显示每隔一行。我究竟做错了什么?
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);
答案 0 :(得分:2)
您在循环中使用imputFile.nextDouble()
两次:
grade = imputFile.nextDouble();
//....
sum += imputFile.nextDouble();
尝试将最后一行更改为
sum += grade;
使用已读取的值,而不是读取下一个值。