
有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,并存到磁盘中
发布日期:2021-05-14 13:25:01
浏览次数:15
分类:精选文章
本文共 3721 字,大约阅读时间需要 12 分钟。
学生信息处理系统
Java程序:学生数据读取与存储
以下是基于Java语言的学生数据处理程序,该程序用于读取多个学生的成绩信息,计算平均值,并将结果存储到文件中。
文件操作示例代码
public class MainStudentData {
public static void main(String[] args) { Student[] students = new Student[5]; Scanner scanner = new Scanner(System.in); for(int i = 0; i < students.length; i++) { System.out.println("请输入第" + (i + 1) + "个学生学号"); String id = scanner.nextLine(); System.out.println("请输入姓名"); String name = scanner.nextLine(); System.out.println("请输入语文成绩"); int chinese = scanner.nextInt(); System.out.println("请输入数学成绩"); int math = scanner.nextInt(); System.out.println("请输入英语成绩"); int english = scanner.nextInt(); Student student = new Student(); student.setStudentId(id); student.setName(name); student.setChinese(chinese); student.setMath(math); student.setEnglish(english); student.setAverage((chinese + math + english) / 3.0); students[i] = student; } try { File scoreFile = new File("Score.txt"); if(!scoreFile.exists()) { scoreFile.createNewFile(); } FileOutputStream fileOutputStream = new FileOutputStream(scoreFile); OutputStreamWriter writer = new OutputStreamWriter(fileOutputStream, "gbk"); writer.write("学号\t姓名\t语文\t数学\t英语\t平均值\t\r\n"); for(int i = 0; i < students.length; i++) { Student student = students[i]; writer.write("\r\n"); writer.write(student.getStudentId() + "\t"); writer.write(student.getName() + "\t"); writer.write(String.valueOf(student.getChinese()) + "\t"); writer.write(String.valueOf(student.getMath()) + "\t"); writer.write(String.valueOf(student.getEnglish()) + "\t"); writer.write(String.valueOf(student.getAverage()) + "\t"); writer.write("\r\n"); } writer.flush(); } catch(FileNotFoundException e) { e.printStackTrace(); } catch(UnsupportedEncodingException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } finally { try { writer.close(); fileOutputStream.close(); } catch(Exception e) { e.printStackTrace(); } }}
}
学生类
public class Student {
private String id;private String name;private int math;private int chinese;private int english;private double avg;public Student() { super();}public Student(String id, String name, int chinese, int math, int english) { this.id = id; this.name = name; this.chinese = chinese; this.math = math; this.english = english; theAvg = (chinese + math + english) / 3.0;}public String getStudentId() { return this.id;}public void setStudentId(String id) { this.id = id;}public String getName() { return this.name;}public void setName(String name) { this.name = name;}public int getChinese() { return this.chinese;}public void setChinese(int chinese) { this.chinese = chinese;}public int getMath() { return this.math;}public void setMath(int math) { this.math = math;}public int getEnglish() { return this.english;}public void setEnglish(int english) { this.english = english;}public double getAverage() { return this.avg;}public String toString() { return "Student [id=" + id + ", name=" + name + ", chinese=" + chinese + ", math=" + math + ", english=" + english + ", avg=" + avg + "]";}
}
以上代码展示了一个学生信息管理系统,主要功能是读取学生的学号、姓名及三门课成绩信息,计算平均成绩,并将结果保存到指定的文本文件中。
注:请确保在使用上述代码时,相应的包和必要的权限已经申请,并且已创建好"Score.txt"文件或允许程序写入该路径。建议在实际运行前仔细检查路径配置和相关权限设置。
发表评论
最新留言
感谢大佬
[***.8.128.20]2025年05月01日 15时47分25秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
大数据在不同领域的应用
2019-03-11
页面置换算法
2019-03-11
推荐系统资料
2019-03-11
文件系统的层次结构
2019-03-11
减少磁盘延迟时间的方法
2019-03-11
vue(渐进式前端框架)
2019-03-11
权值初始化和与损失函数
2019-03-11
案例讨论
2019-03-11
传输层基本功能
2019-03-11
问题的计算复杂度:排序问题
2019-03-11
算法的伪码表示
2019-03-11
递推方程与算法分析
2019-03-11
主定理的应用
2019-03-11
最优装载问题
2019-03-11
最大团问题
2019-03-11
圆排列问题
2019-03-11
课程总结
2019-03-11
认识CMake及应用
2019-03-11
CMake的主体框架
2019-03-11
微积分(三)
2019-03-11