用Java语言完成 (1)输入8名学生姓名、学号、性别、籍贯、所在学院; (2)统计籍贯为陕西的学生人数,以及男生和女生的人数; (3)统计每个学院的学生人数; (4)输入学号查询学生的信息; (5)将学生信息保存在文本文件中。具体代码
时间: 2023-02-07 17:09:23 浏览: 82
Java中没有类似于struct的数据结构,但是可以使用Java Bean或者Java类来表示一个学生的信息。
以下是一个可行的方法的代码示例:
```
import java.util.Scanner;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import java.io.FileWriter;
import java.io.IOException;
public class StudentInfo {
// 定义学生类
public static class Student {
public String name;
public String id;
public String gender;
public String origin;
public String college;
public Student(String name, String id, String gender, String origin, String college) {
this.name = name;
this.id = id;
this.gender = gender;
this.origin = origin;
this.college = college;
}
}
public static void main(String[] args) {
// 输入8名学生信息
Scanner scanner = new Scanner(System.in);
List<Student> students = new ArrayList<>();
for (int i = 0; i < 8; i++) {
System.out.println("请输入第" + (i+1) + "名学生的信息:");
System.out.print("姓名:");
String name = scanner.nextLine();
System.out.print("学号:");
String id = scanner.nextLine();
System.out.print("性别:");
String gender = scanner.nextLine();
System.out.print("籍贯:");
String origin = scanner.nextLine();
System.out.print("所在学院:");
String college = scanner.nextLine();
students.add(new Student(name, id, gender, origin, college));
}
// 统计籍贯为陕西的学生人数,以及男生和女生的人数
int numShaanxi = 0;
int numMale = 0;
int numFemale = 0;
for (Student student : students) {
if (student.origin.equals("陕西")) {
numShaanxi++;
}
if (student.gender.equals("男")) {
numMale++;
} else {
numFemale++;
}
}
System.out.println("籍贯为陕西的学生人数:" + numShaanxi);
System.out.println("
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)