java实现1、有如下数据,请按要求完成如下功能: 姓名 性别 生日 居住地 小乔妹 女 2000-10-10 四川省-成都市 大乔哥 男 1998-01-10 四川省-绵阳市 山上妹 女 1990-05-17 四川省-德阳市 小乔妹 女 2000-10-10 四川省-成都市 大乔哥 男 1998-01-10 四川省-绵阳市 井口哥 女 2005-04-23 四川省-成都市 渡边妹 女 2002-08-10 四川省-绵阳市 小野哥 男 2001-11-11 四川省-成都市 松下妹 女 2004-07-17 四川省-德阳市 龟田哥 男 1997-06-12 四川省-成都市 波波妹 女 2000-10-10 四川省-绵阳市 小野哥 男 2001-11-11 四川省-成都市 (4)、将stu.txt中的学生按年龄升序排列,覆盖原来stu.txt里面的信息 (5)、统计男女各有多少人 (6)、统计成都市,绵阳市,德阳市,每个居住地学生占总人数的百分比
时间: 2023-08-06 09:02:27 浏览: 109
JAVA版身份证获取性别、出生日期及年龄
5星 · 资源好评率100%
这是一个Java的编程问题,可以使用Java的IO流、集合和排序等知识来实现。以下是代码实现:
```java
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class StudentManager {
private List<Student> students = new ArrayList<>();
public void loadDataFromFile(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String line = null;
while ((line = reader.readLine()) != null) {
String[] data = line.split("\t");
String name = data[0];
String gender = data[1];
Date birthday = new SimpleDateFormat("yyyy-MM-dd").parse(data[2]);
String address = data[3];
students.add(new Student(name, gender, birthday, address));
}
reader.close();
} catch (IOException | ParseException e) {
e.printStackTrace();
}
}
public void saveDataToFile(String fileName) {
File file = new File(fileName);
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(file));
for (Student student : students) {
writer.write(student.toString());
writer.newLine();
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void sortByAge() {
Collections.sort(students, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
return s1.getBirthday().compareTo(s2.getBirthday());
}
});
}
public Map<String, Integer> countGender() {
Map<String, Integer> genderCount = new HashMap<>();
for (Student student : students) {
String gender = student.getGender();
if (genderCount.containsKey(gender)) {
genderCount.put(gender, genderCount.get(gender) + 1);
} else {
genderCount.put(gender, 1);
}
}
return genderCount;
}
public Map<String, Double> countAddressPercentage() {
Map<String, Double> addressPercentage = new HashMap<>();
int total = students.size();
for (Student student : students) {
String address = student.getAddress();
if (addressPercentage.containsKey(address)) {
addressPercentage.put(address, addressPercentage.get(address) + 1.0 / total);
} else {
addressPercentage.put(address, 1.0 / total);
}
}
return addressPercentage;
}
public static void main(String[] args) {
StudentManager manager = new StudentManager();
manager.loadDataFromFile("stu.txt");
manager.sortByAge();
manager.saveDataToFile("stu.txt");
Map<String, Integer> genderCount = manager.countGender();
System.out.println("男:" + genderCount.get("男") + " 女:" + genderCount.get("女"));
Map<String, Double> addressPercentage = manager.countAddressPercentage();
for (String address : addressPercentage.keySet()) {
System.out.println(address + ":" + addressPercentage.get(address) * 100 + "%");
}
}
}
class Student {
private String name;
private String gender;
private Date birthday;
private String address;
public Student(String name, String gender, Date birthday, String address) {
this.name = name;
this.gender = gender;
this.birthday = birthday;
this.address = address;
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
public Date getBirthday() {
return birthday;
}
public String getAddress() {
return address;
}
@Override
public String toString() {
return name + "\t" + gender + "\t" + new SimpleDateFormat("yyyy-MM-dd").format(birthday) + "\t" + address;
}
}
```
其中,`loadDataFromFile`方法从文件中读取数据到`List<Student>`中,`saveDataToFile`方法将`List<Student>`中的数据保存到文件中,`sortByAge`方法按照年龄升序排列学生信息,`countGender`方法统计男女人数,`countAddressPercentage`方法统计每个居住地学生占总人数的百分比。`main`方法中,首先加载数据,然后按照年龄排序并保存到文件中,然后统计男女人数和每个居住地学生占总人数的百分比,并输出结果。
阅读全文