Java 成绩分析绘图_(java)编写一个学生成绩分析程序

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 19:35   976   0

展开全部

//学生类

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) {

e5a48de588b662616964757a686964616f31333431353839this.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{

@Override

public int compare(Student o1, Student o2) {

return o1.getTotal()

}

}

public class Test {

public static void main(String[] args) {

String filename = "res/student.txt";

System.out.println("读取" + filename + "文件的数据");

List 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()

System.out.println(student.getName() + " 数学成绩不及格");

nums ++;

}

}

System.out.println("数学不及格总共有" + nums + "人");

}

//从文件中读取数据

private static List readStudnetInfoFromFile(String filename) {

File file = new File(filename);

if (!file.exists()) {

return null;

}

List 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

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP