编写一个java程序,实现一下功能,要求 设计一个学生student类,包含学号、姓名、出生日期、性别等字段 从input.txt(从附件下载)中读取其中的学生信息,并存入学生集合list中 对学生集合list进行排序,按照出生日期从早到晚排序(即年龄由大到小) 将排序后的学生集合list输出到output.txt文件中,输出格式同input.txt:一行一个学生,依次输出学号、姓名、出生日期、性别并用空格隔开

时间: 2024-02-15 12:01:43 浏览: 24
好的,根据您的要求,我为您编写了以下 Java 程序。请注意,代码中的注释会帮助您理解程序的工作原理。 ```java import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Scanner; class Student { private int id; // 学号 private String name; // 姓名 private int birthYear; // 出生年份 private int birthMonth; // 出生月份 private int birthDay; // 出生日期 private String gender; // 性别 public Student(int id, String name, int birthYear, int birthMonth, int birthDay, String gender) { this.id = id; this.name = name; this.birthYear = birthYear; this.birthMonth = birthMonth; this.birthDay = birthDay; this.gender = gender; } public int getId() { return id; } public String getName() { return name; } public int getBirthYear() { return birthYear; } public int getBirthMonth() { return birthMonth; } public int getBirthDay() { return birthDay; } public String getGender() { return gender; } @Override public String toString() { return id + " " + name + " " + birthYear + "-" + birthMonth + "-" + birthDay + " " + gender; } } class StudentComparator implements Comparator<Student> { @Override public int compare(Student s1, Student s2) { // 按出生日期从早到晚排序 if (s1.getBirthYear() != s2.getBirthYear()) { return s1.getBirthYear() - s2.getBirthYear(); } else if (s1.getBirthMonth() != s2.getBirthMonth()) { return s1.getBirthMonth() - s2.getBirthMonth(); } else { return s1.getBirthDay() - s2.getBirthDay(); } } } public class Main { public static void main(String[] args) { // 读取 input.txt 中的学生信息,存入学生集合 list 中 ArrayList<Student> list = new ArrayList<>(); try { File inputFile = new File("input.txt"); Scanner scanner = new Scanner(inputFile); while (scanner.hasNextLine()) { String line = scanner.nextLine(); String[] fields = line.split(" "); int id = Integer.parseInt(fields[0]); String name = fields[1]; String[] birthFields = fields[2].split("-"); int birthYear = Integer.parseInt(birthFields[0]); int birthMonth = Integer.parseInt(birthFields[1]); int birthDay = Integer.parseInt(birthFields[2]); String gender = fields[3]; Student student = new Student(id, name, birthYear, birthMonth, birthDay, gender); list.add(student); } scanner.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } // 对学生集合 list 进行排序 Collections.sort(list, new StudentComparator()); // 将排序后的学生集合 list 输出到 output.txt 文件中 try { FileWriter writer = new FileWriter("output.txt"); for (Student student : list) { writer.write(student.toString() + "\n"); } writer.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 请将上述代码保存为 Main.java 文件,并将 input.txt 文件放在同一目录下。运行程序后,它会自动读取 input.txt 文件中的学生信息,按照出生日期从早到晚排序,并将排序后的学生集合输出到 output.txt 文件中。

相关推荐

最新推荐

recommend-type

typora.zip

typora.zip
recommend-type

系统中常用的软件版本说明.docx

系统中常用的软件版本说明.docx
recommend-type

c&c++学生档案管理系统,个人收支系统,职工管理系统等.rar

C语言、C++、delphi各种案例
recommend-type

orca算法的matlab仿真源代码.zip

orca算法的matlab仿真源代码.zip orca算法的matlab仿真源代码.ziporca算法的matlab仿真源代码.ziporca算法的matlab仿真源代码.ziporca算法的matlab仿真源代码.ziporca算法的matlab仿真源代码.ziporca算法的matlab仿真源代码.ziporca算法的matlab仿真源代码.ziporca算法的matlab仿真源代码.ziporca算法的matlab仿真源代码.ziporca算法的matlab仿真源代码.ziporca算法的matlab仿真源代码.ziporca算法的matlab仿真源代码.ziporca算法的matlab仿真源代码.ziporca算法的matlab仿真源代码.ziporca算法的matlab仿真源代码.ziporca算法的matlab仿真源代码.ziporca算法的matlab仿真源代码.ziporca算法的matlab仿真源代码.ziporca算法的matlab仿真源代码.ziporca算法的matlab仿真源代码.ziporca算法的matlab仿真源代码.ziporca算法的matla
recommend-type

JSJAVA卡片场景能力差异简析.docx

JSJAVA卡片场景能力差异简析
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

解释这行代码 c = ((double)rand() / RAND_MAX) * (a + b - fabs(a - b)) + fabs(a - b);

这行代码是用于生成 a 和 b 之间的随机数。首先,它使用 rand() 函数生成一个 [0,1) 之间的随机小数,然后将这个小数乘以 a、b 范围内的差值,再加上 a 和 b 中的较小值。这可以确保生成的随机数大于等于 a,小于等于 b,而且不会因为 a 和 b 之间的差距过大而导致难以生成足够多的随机数。最后,使用 fabs() 函数来确保计算结果是正数。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。