【发布时间】:2019-04-19 02:41:14
【问题描述】:
我需要让程序循环到文件末尾。每次循环运行时,它都应该捕获学生的姓名和考试成绩。程序应该知道它已经达到学生分数的末尾的方式是它找到“-1”。然后它应该找到他们的平均成绩和字母等级。最后,程序应该打印出一个包含整个班级摘要的文件。
我已经让它为第一个学生工作,但现在我需要让它工作到文件结束。我知道这可能不是解决此问题的最有效方法,但这是我能想到的唯一方法。
public static void main(String[] args) throws FileNotFoundException {
//Declare String Variables
String name = null;
String grade;
String inFile;
String outFile;
PrintWriter outputFile;
//Declare Numerical Variables
double score1 = 0;
double score2 = 0;
double score3 = 0;
double score4 = 0;
double score5 = 0;
double score6 = 0;
double score7 = 0;
double score8 = 0;
double count = 0;
double average;
//Capture Student Info
while(scanFile.hasNext()){
name = scanFile.nextLine();
if (scanFile.hasNextInt()){
score1 = scanFile.nextInt();
count++;
if (score1 == -1){
}
break;
else if (score1 != -1)
score2 = scanFile.nextInt();
count++;
if (score2 == -1){
count--;
break;
}
else if (score2 != -1)
score3 = scanFile.nextInt();
count++;
if (score3 == -1){
count--;
break;
}
else if (score3 != -1)
score4 = scanFile.nextInt();
count++;
if (score4 == -1){
count--;
break;
}
else if (score4 != -1)
score5 = scanFile.nextInt();
count++;
if (score5 == -1){
count--;
break;
}
else if (score5 != -1)
score6 = scanFile.nextInt();
count++;
if (score6 == -1){
count--;
break;
}
else if (score6 != -1)
score7 = scanFile.nextInt();
count++;
if (score7 == -1){
count--;
break;
}
else if (score7 != -1)
score8 = scanFile.nextInt();
count++;
}
}
//Processed data for student
average = findAverage(score1, score2, score3, score4, score5, score6, score7, score8, count);
grade = letterGrade(average);
//Printing Output File
outputFile.print("Grade Report for Introduction to Programming I\n\n");
outputFile.print("This program will process test scores.");
//Output
printFile(outputFile, name, count, average, grade);
//}
outputFile.close();
}
示例输入文件
John Sweet
87 76 90 100 -1
Ali Hassan
-1
Willy Nilly
73 63 74 70 -1
Juju Smith Jr.
89 90 78 88 -1
Karl Kavington III
90 100 80 70 -1
Lary Howard Holiday
80 77 67 67 -1
Leo Gordon
56 88 780 77 -1
Jessy Brown
-1
Mr. Perfect
100 100 100 100 -1
Mr. Missing It All
0 0 0 0 0 0 -1
【问题讨论】:
-
你知道接口
java.util.List及其实现如java.util.ArrayList吗? -
这段代码还能编译吗?我会感到惊讶。
标签: java loops java.util.scanner