java编程一个学生管理系统程序
时间: 2023-06-12 12:02:22 浏览: 102
java编写的学生管理系统
好的,我可以为您介绍一个简单的学生管理系统程序,但需要说明的是,这只是一个示范程序,需要您自行完善和修改以适应实际需求。
首先,我们需要设计学生类,包括学生的基本属性如姓名,学号等,以及相关方法。以下是一个简单的示例:
```java
public class Student {
private int id;
private String name;
private int age;
private String gender;
public Student(int id, String name, int age, String gender) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "学生信息{" +
"学号=" + id +
", 姓名='" + name + '\'' +
", 年龄=" + age +
", 性别='" + gender + '\'' +
'}';
}
}
```
然后,我们需要设计一个学生管理类,用于添加,删除,修改和查询学生信息。以下是一个简单的示例:
```java
import java.util.ArrayList;
public class StudentManager {
private ArrayList<Student> students;
public StudentManager() {
students = new ArrayList<>();
}
public void addStudent(Student stu) {
students.add(stu);
}
public void removeStudent(int id) {
for (Student s : students) {
if (s.getId() == id) {
students.remove(s);
break;
}
}
}
public void updateStudent(Student stu) {
for (Student s : students) {
if (s.getId() == stu.getId()) {
s.setName(stu.getName());
s.setAge(stu.getAge());
s.setGender(stu.getGender());
break;
}
}
}
public Student findStudent(int id) {
for (Student s : students) {
if (s.getId() == id) {
return s;
}
}
return null;
}
public void displayAllStudents() {
for (Student s : students) {
System.out.println(s);
}
}
}
```
最后,我们可以在主函数中调用学生管理类的方法来实现学生信息的增删改查等操作。以下是一个简单的示例:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
StudentManager manager = new StudentManager();
Scanner scanner = new Scanner(System.in);
//添加学生
System.out.println("请输入要添加的学生信息:");
System.out.print("学号:");
int id = scanner.nextInt();
System.out.print("姓名:");
String name = scanner.next();
System.out.print("年龄:");
int age = scanner.nextInt();
System.out.print("性别:");
String gender = scanner.next();
Student stu = new Student(id, name, age, gender);
manager.addStudent(stu);
//删除学生
System.out.println("请输入要删除的学生学号:");
int delId = scanner.nextInt();
manager.removeStudent(delId);
//修改学生
System.out.println("请输入要修改的学生信息:");
System.out.print("学号:");
int updateId = scanner.nextInt();
System.out.print("姓名:");
String newName = scanner.next();
System.out.print("年龄:");
int newAge = scanner.nextInt();
System.out.print("性别:");
String newGender = scanner.next();
Student newStu = new Student(updateId, newName, newAge, newGender);
manager.updateStudent(newStu);
//查询学生
System.out.println("请输入要查询的学生学号:");
int findId = scanner.nextInt();
Student findStu = manager.findStudent(findId);
System.out.println(findStu);
//显示所有学生
manager.displayAllStudents();
}
}
```
以上就是一个简单的学生管理系统程序的示例。您可以根据实际需求进行修改和完善。希望能对您有所帮助。
阅读全文