用Java编写一个学生、老师、科目数据输入和显示程序,学生数据里有编号、姓名、班级,老师有姓名、科目、部门并且搜老师可以查出对应的科目和学生,搜科目列出相对应的老师和学生
时间: 2023-07-10 12:07:54 浏览: 76
以下是一个简单的示例程序,可以实现学生、老师、科目数据的输入和显示:
```java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class StudentTeacherSubjectProgram {
// 定义学生类
public static class Student {
private int id;
private String name;
private String className;
public Student(int id, String name, String className) {
this.id = id;
this.name = name;
this.className = className;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
}
// 定义老师类
public static class Teacher {
private String name;
private String subject;
private String department;
public Teacher(String name, String subject, String department) {
this.name = name;
this.subject = subject;
this.department = department;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 定义学生数据列表和老师数据列表
ArrayList<Student> studentList = new ArrayList<>();
ArrayList<Teacher> teacherList = new ArrayList<>();
// 定义老师和科目的对应关系和学生和老师的对应关系
Map<String, ArrayList<Teacher>> subjectTeacherMap = new HashMap<>();
Map<Teacher, ArrayList<Student>> teacherStudentMap = new HashMap<>();
// 输入学生数据
System.out.println("请输入学生数据(输入 q 结束):");
while (true) {
System.out.print("学生编号:");
String input = scanner.nextLine();
if (input.equals("q")) {
break;
}
int id = Integer.parseInt(input);
System.out.print("学生姓名:");
String name = scanner.nextLine();
System.out.print("学生班级:");
String className = scanner.nextLine();
Student student = new Student(id, name, className);
studentList.add(student);
}
// 输入老师数据
System.out.println("请输入老师数据(输入 q 结束):");
while (true) {
System.out.print("老师姓名:");
String input = scanner.nextLine();
if (input.equals("q")) {
break;
}
String name = input;
System.out.print("教授科目:");
String subject = scanner.nextLine();
System.out.print("所在部门:");
String department = scanner.nextLine();
Teacher teacher = new Teacher(name, subject, department);
teacherList.add(teacher);
// 更新老师和科目的对应关系
if (subjectTeacherMap.containsKey(subject)) {
ArrayList<Teacher> teacherArrayList = subjectTeacherMap.get(subject);
teacherArrayList.add(teacher);
} else {
ArrayList<Teacher> teacherArrayList = new ArrayList<>();
teacherArrayList.add(teacher);
subjectTeacherMap.put(subject, teacherArrayList);
}
}
// 输入学生和老师的对应关系
System.out.println("请输入学生和老师的对应关系(输入 q 结束):");
while (true) {
System.out.print("学生编号:");
String input = scanner.nextLine();
if (input.equals("q")) {
break;
}
int studentId = Integer.parseInt(input);
System.out.print("老师姓名:");
String teacherName = scanner.nextLine();
// 查找对应的老师
Teacher teacher = null;
for (Teacher t : teacherList) {
if (t.getName().equals(teacherName)) {
teacher = t;
break;
}
}
if (teacher != null) {
// 更新老师和学生的对应关系
if (teacherStudentMap.containsKey(teacher)) {
ArrayList<Student> studentArrayList = teacherStudentMap.get(teacher);
studentArrayList.add(new Student(studentId, "", ""));
} else {
ArrayList<Student> studentArrayList = new ArrayList<>();
studentArrayList.add(new Student(studentId, "", ""));
teacherStudentMap.put(teacher, studentArrayList);
}
} else {
System.out.println("没有找到对应的老师!");
}
}
// 显示学生数据
System.out.println("学生数据:");
for (Student student : studentList) {
System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getClassName());
}
// 显示老师数据
System.out.println("老师数据:");
for (Teacher teacher : teacherList) {
System.out.println(teacher.getName() + "\t" + teacher.getSubject() + "\t" + teacher.getDepartment());
}
// 显示科目和老师的对应关系
System.out.println("科目和老师的对应关系:");
for (Map.Entry<String, ArrayList<Teacher>> entry : subjectTeacherMap.entrySet()) {
String subject = entry.getKey();
ArrayList<Teacher> teacherArrayList = entry.getValue();
System.out.print(subject + ":");
for (Teacher teacher : teacherArrayList) {
System.out.print(teacher.getName() + " ");
}
System.out.println();
}
// 显示科目和学生的对应关系
System.out.println("科目和学生的对应关系:");
for (Map.Entry<Teacher, ArrayList<Student>> entry : teacherStudentMap.entrySet()) {
Teacher teacher = entry.getKey();
ArrayList<Student> studentArrayList = entry.getValue();
System.out.print(teacher.getSubject() + ":" + teacher.getName() + " ");
for (Student student : studentArrayList) {
System.out.print(student.getId() + " ");
}
System.out.println();
}
}
}
```
在程序中,我们定义了学生类和老师类来表示学生和老师的数据。我们使用 ArrayList 来存储学生和老师的数据,使用 HashMap 来存储科目和老师的对应关系和老师和学生的对应关系。
在程序中,我们首先输入学生和老师的数据,并将它们分别存储在 studentList 和 teacherList 中。然后,我们输入学生和老师的对应关系,并将它们存储在 teacherStudentMap 中。
最后,我们使用 for 循环来遍历 studentList、teacherList、subjectTeacherMap 和 teacherStudentMap,并输出它们的内容。
注意,在程序中,输入 q 表示结束输入。
阅读全文