我正在努力解决一个特定问题,但鉴于这在技术上是家庭作业,我想了解我做错了什么,我希望有一个更通用的解决方案。一些警告:我必须使用扫描仪类,并且我的数据不在数组或任何东西中。我从网站上的阅读中知道 BufferedReading 是首选。从我读过的内容来看,我想我也更喜欢它。但这不是我可以在这里工作的。
我正在尝试从数据文件中读取数据,然后对该文件执行一些操作。数据是这样的:
1234 55 64 75
1235 44 32 12
...
nnnn xx yy zz
0
2234 47 57 67
2235 67 67 67
...
nnnn xx yy zz
0
每行是一个 ID,后跟三个等级。每个类都以零线终止,然后 while 循环从顶部开始:
while (classContinues == true) {
//get the student's data and assign it in the program
studentID = inputFile.nextInt();
programGrade = inputFile.nextInt();
midtermGrade = inputFile.nextInt();
finalGrade = inputFile.nextInt();
// in here I'm doing other stuff but I don't need help with that
// check if the class has more students
if (inputFile.nextInt() == 0) {
classContinues = false;
} else {
classContinues = true;
inputFile.nextLine(); // eat the rest of the line
}
}
现在,当你像这样运行代码时,它会打印出我想要的输出,但它会跳过每隔一行的数据。删除 inputFile.nextLine(); 它会跳过第二个学生 ID,然后弄乱所有其他输出。所以我想我想知道我做错了什么——我应该如何在不吃下一个学生证的情况下检查下一个整数是否为零?