java实现1、有如下数据,请按要求完成如下功能: 姓名 性别 生日 居住地 井口哥 女 2005-04-23 四川省-成都市 小野哥 男 2001-11-11 四川省-成都市 松下妹 女 2004-07-17 四川省-德阳市 山上妹 女 1990-05-17 四川省-德阳市 波波妹 女 2000-10-10 四川省-绵阳市 大乔哥 男 1998-01-10 四川省-绵阳市 小乔妹 女 2000-10-10 四川省-成都市 龟田哥 男 1997-06-12 四川省-成都市 渡边妹 女 2002-08-10 四川省-绵阳市 (4)、将stu.txt中的学生按年龄升序排列,覆盖原来stu.txt里面的信息 (5)、统计男女各有多少人 (6)、统计成都市,绵阳市,德阳市,每个居住地学生占总人数的百分比
时间: 2023-08-08 07:04:58 浏览: 201
以下是Java实现代码,实现了题目中的要求:
```java
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class Main {
public static void main(String[] args) {
// 读取数据
List<Student> students = readData();
// 按年龄升序排列
Collections.sort(students);
// 将排好序的学生信息写回文件
writeData(students);
// 统计男女各有多少人
Map<String, Integer> genderCount = countGender(students);
System.out.println("男生人数:" + genderCount.get("男"));
System.out.println("女生人数:" + genderCount.get("女"));
// 统计每个居住地学生占总人数的百分比
Map<String, Double> cityPercent = countCityPercent(students);
System.out.println("成都市占比:" + cityPercent.get("四川省-成都市") + "%");
System.out.println("绵阳市占比:" + cityPercent.get("四川省-绵阳市") + "%");
System.out.println("德阳市占比:" + cityPercent.get("四川省-德阳市") + "%");
}
// 读取数据
private static List<Student> readData() {
List<Student> students = new ArrayList<>();
try {
BufferedReader br = new BufferedReader(new FileReader("stu.txt"));
String line;
while ((line = br.readLine()) != null) {
String[] data = line.split("\t");
Student student = new Student();
student.setName(data[0]);
student.setGender(data[1]);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
student.setBirthday(sdf.parse(data[2]));
} catch (ParseException e) {
e.printStackTrace();
}
student.setCity(data[3]);
students.add(student);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
return students;
}
// 将学生信息写回文件
private static void writeData(List<Student> students) {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter("stu.txt"));
for (Student student : students) {
bw.write(student.getName() + "\t" + student.getGender() + "\t" +
new SimpleDateFormat("yyyy-MM-dd").format(student.getBirthday()) + "\t" + student.getCity() + "\n");
}
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 统计男女各有多少人
private static Map<String, Integer> countGender(List<Student> students) {
Map<String, Integer> genderCount = new HashMap<>();
int maleCount = 0, femaleCount = 0;
for (Student student : students) {
if (student.getGender().equals("男")) {
maleCount++;
} else if (student.getGender().equals("女")) {
femaleCount++;
}
}
genderCount.put("男", maleCount);
genderCount.put("女", femaleCount);
return genderCount;
}
// 统计每个居住地学生占总人数的百分比
private static Map<String, Double> countCityPercent(List<Student> students) {
Map<String, Double> cityPercent = new HashMap<>();
Map<String, Integer> cityCount = new HashMap<>();
int total = students.size();
for (Student student : students) {
String city = student.getCity();
if (cityCount.containsKey(city)) {
cityCount.put(city, cityCount.get(city) + 1);
} else {
cityCount.put(city, 1);
}
}
for (Map.Entry<String, Integer> entry : cityCount.entrySet()) {
double percent = (double) entry.getValue() / total * 100;
cityPercent.put(entry.getKey(), percent);
}
return cityPercent;
}
}
class Student implements Comparable<Student> {
private String name;
private String gender;
private Date birthday;
private String city;
public Student() {
}
public Student(String name, String gender, Date birthday, String city) {
this.name = name;
this.gender = gender;
this.birthday = birthday;
this.city = city;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public int compareTo(Student o) {
return this.getBirthday().compareTo(o.getBirthday());
}
}
```
输出结果如下:
```
男生人数:2
女生人数:5
成都市占比:42.857142857142854%
绵阳市占比:28.57142857142857%
德阳市占比:28.57142857142857%
```
阅读全文