(java)编写一个学生成绩分析程序

(java) 编写一个学生成绩分析程序,学生成绩由文件输入获取,每个学生成绩包括学号、姓名、数学、物理、英语。一个学生数据占1行。数据导入到数组列表中。
1)按总分由高到低排序输出;Integer.parseInt(str)
2)统计数学不及格学生人数。

求详细代码

//学生类
public class Student {
private String NO;
private String name;
private int math;
private int physics;
private int english;
private int total = 0;
public Student(String NO, String name, int math, int physics, int english) {
this.NO = NO;
this.name = name;
this.math = math;
this.physics = physics;
this.english = english;
total = math + physics + english;
}
public String getNO() {
return NO;
}
public void setNO(String nO) {
NO = nO;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getPhysics() {
return physics;
}
public void setPhysics(int physics) {
this.physics = physics;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
@Override
public String toString() {
return "[学号=" + NO + ", 姓名=" + name + ", 数学成绩=" + math + ", 物理成绩=" + physics + ", 英语="
+ english + ", 总分=" + total + "]";
}


}

//排序
public static class CompareStudent implements Comparator<Student>{

@Override
public int compare(Student o1, Student o2) {

return o1.getTotal() < o2.getTotal() ? 1 : o1.getTotal() == o2.getTotal() ? 0 : -1;
}

}

public class Test {

public static void main(String[] args) {
String filename = "res/student.txt";
System.out.println("读取" + filename + "文件的数据");
List<Student> result = readStudnetInfoFromFile(filename);

//排序
result.sort(new CompareStudent());

System.out.println("排序输出");
for (Student student : result) {
System.out.println(student);
}
System.out.println();
//不及格
System.out.println("数学不及格");
int nums = 0;
for (Student student : result) {
if(student.getMath() < 60) {
System.out.println(student.getName() + " 数学成绩不及格");
nums ++;
}
}
System.out.println("数学不及格总共有" + nums + "人");
}



//从文件中读取数据
private static List<Student> readStudnetInfoFromFile(String filename) {
File file = new File(filename);
if (!file.exists()) {
return null;
}
List<Student> result = new ArrayList<>();
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader(file);
br = new BufferedReader(fr);
String line = null;
while ((line = br.readLine()) != null) {
String[] infos = line.split("_");
Student stu = new Student(infos[0], infos[1], Integer.parseInt(infos[2]), Integer.parseInt(infos[3]),
Integer.parseInt(infos[4]));
result.add(stu);
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {

try {
if (br != null) {
br.close();
}
if(fr != null) {
fr.close();
}
} catch (IOException e) {
e.printStackTrace();
}

}

return result;
}


//数据文件  注意文件路径
1_张三_60_70_80
2_李四_58_58_73
3_王二_63_75_85
4_莉莉_46_75_88
5_小明_85_76_56

温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-06-01
又不是很难 自己不会吗?追问

自己自学,不太会的

追答

呃呃呃 虽然不是很难 但是也费时间