完整的用Java语言写出满足下列条件的代码(1)输入8名学生姓名、学号、性别、籍贯、所在学院; (2)统计籍贯为陕西的学生人数,以及男生和女生的人数; (3)统计每个学院的学生人数; (4)输入学号查询学生的信息; (5)将学生信息保存在文本文件中。
时间: 2023-02-07 07:01:54 浏览: 76
首先需要创建一个学生类,包含姓名、学号、性别、籍贯、所在学院五个属性:
```
public class Student {
private String name;
private String id;
private String gender;
private String hometown;
private String college;
// 省略构造函数、getter 和 setter 方法
}
```
然后我们可以使用一个学生管理类来统计信息,并通过输入学号查询学生信息。这里需要使用 Java 的 Scanner 类来读取用户的输入。
```
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManager {
// 使用 ArrayList 存储学生信息
private ArrayList<Student> students = new ArrayList<>();
// 添加学生
public void addStudent(Student student) {
students.add(student);
}
// 统计籍贯为陕西的学生人数,以及男生和女生的人数
public void statistics() {
int shanxiCount = 0;
int maleCount = 0;
int femaleCount = 0;
for (Student student : students) {
if (student.getHometown().equals("陕西")) {
shanxiCount++;
}
if (student.getGender().equals("男")) {
maleCount++;
} else if (student.getGender().equals("女")) {
femaleCount++;
}
}
System.out.println("籍贯为陕西的学生人数: " + shanxiCount);
System.out.println("男生人数: " + maleCount);
System.out.println("女生人数: " + femaleCount);
}
// 统计每个学院的学生人数
public void statisticsByCollege() {
// 使用 HashMap 统计每个学院的学生人数
HashMap<String, Integer> collegeCounts = new HashMap<>();
for (Student student : students) {
String college = student.getCollege();
if (collegeCounts.containsKey(college)) {
collegeCounts.put(college, collegeCounts.get(college) + 1);
} else {
collegeCounts.
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)