我写了这个简单的脚本,它读取文本文件,然后将数据添加到数组中,但我只想打印超过90个学分的学生的名字和姓氏
Student[] student = new Student[50];
Scanner userin, filein;
String filename;
int numStudents;
// Get the filename from the user, with exception handling to deal with incorrect file names
userin = new Scanner(System.in);
filename = null;
filein = null;
filename = Students.txt;
try {
filein = new Scanner(new FileReader(filename)); // try to open the file
} catch (FileNotFoundException e) { // failed to open the file
System.out.println("Invalid file - try again");
filename = null;
}
numStudents = 0;
while (filein.hasNext()){
String lastName = filein.next();
String firstName = filein.next();
double gpa = filein.next();
int Credits = filein.next();
student[numStudents++] = new Student(lastName,firstName,gpa,Credits);
}
int i=0;
do
{
System.out.println(student[i].toString());
i++;
}
while ((student[i] != null)&&(i <= student.length));
答案 0 :(得分:2)
“脚本”?
这是一些丑陋的代码。格式化和结构化问题。我建议将来更加关注它。随着您的程序(又称“脚本”)变得更加复杂,它将使您的维护生活更轻松。
了解Sun Java coding standards。你现在没有关注他们。
Java中的所有内容都必须是类的一部分。我假设你知道这一点。这必须是你从班级中删除的片段。
你的代码太乱了,不用担心让它变好。这个工作:
if (student[i].getCredits() >= 90)
System.out.println(student[i].toString());