21xrx.com
2024-12-22 23:43:47 Sunday
登录
文章检索 我的文章 写文章
我最近在学习Java编程
2023-06-14 23:27:18 深夜i     --     --
Java编程 学生管理系统 代码例子

我最近在学习Java编程,学到了如何编写学生管理系统,这个系统可以方便管理学生的信息。今天我就来分享一下我的学习心得,希望能对其他想学习Java的朋友有所帮助。

首先,我们需要搭建Java开发环境。我使用的是Eclipse IDE,安装好后就可以开始写代码了。

接下来,需要定义学生信息的类。代码如下:


public class Student {

  private String name;

  private int age;

  private String gender;

  private String major;

  

  public Student(String name, int age, String gender, String major)

    this.name = name;

    this.age = age;

    this.gender = gender;

    this.major = major;

  

  

  public String getName()

    return this.name;

  

  

  public int getAge()

    return this.age;

  

  

  public String getGender()

    return this.gender;

  

  

  public String getMajor()

    return this.major;

  

  

  public void setName(String name)

    this.name = name;

  

  

  public void setAge(int age)

    this.age = age;

  

  

  public void setGender(String gender)

    this.gender = gender;

  

  

  public void setMajor(String major)

    this.major = major;

  

}

在这个类中,我们定义了学生的四个属性:姓名、年龄、性别和专业。然后,使用构造函数和getter/setter方法访问和修改这些属性的值。

然后,我们需要创建一个管理学生信息的类。代码如下:


import java.util.ArrayList;

public class StudentManager {

  private ArrayList students;

  

  public StudentManager() {

    this.students = new ArrayList ();

  }

  

  public ArrayList getStudents()

    return this.students;

  

  

  public void addStudent(Student student) {

    this.students.add(student);

  }

  

  public void removeStudent(Student student) {

    this.students.remove(student);

  }

  

  public void updateStudent(Student student, String name, int age, String gender, String major) {

    student.setName(name);

    student.setAge(age);

    student.setGender(gender);

    student.setMajor(major);

  }

}

这个类中,我们使用了ArrayList来管理学生信息,包括添加、删除和更新学生信息的方法。

最后,我们需要编写一个简单的用户界面来测试我们的学生管理系统。代码如下:


import java.util.Scanner;

public class StudentUI {

  private StudentManager manager;

  private Scanner scanner;

  

  public StudentUI() {

    this.manager = new StudentManager();

    this.scanner = new Scanner(System.in);

  }

  

  public void start() {

    boolean running = true;

    int choice = 0;

    

    while (running) {

      System.out.println("1. Add student");

      System.out.println("2. Remove student");

      System.out.println("3. Update student");

      System.out.println("4. Show all students");

      System.out.println("5. Exit");

      System.out.print("Enter your choice: ");

      

      try {

        choice = scanner.nextInt();

      } catch (Exception e) {

        System.out.println("Invalid choice");

        scanner.next();

        continue;

      }

      

      switch (choice) {

        case 1:

          addStudent();

          break;

        case 2:

          removeStudent();

          break;

        case 3:

          updateStudent();

          break;

        case 4:

          showAllStudents();

          break;

        case 5:

          running = false;

          break;

        default:

          System.out.println("Invalid choice");

          break;

      }

    }

  }

  

  private void addStudent() {

    System.out.print("Enter name: ");

    String name = scanner.next();

    System.out.print("Enter age: ");

    int age = scanner.nextInt();

    System.out.print("Enter gender: ");

    String gender = scanner.next();

    System.out.print("Enter major: ");

    String major = scanner.next();

    

    Student student = new Student(name, age, gender, major);

    manager.addStudent(student);

    

    System.out.println("Student added");

  }

  

  private void removeStudent() {

    System.out.print("Enter name: ");

    String name = scanner.next();

    

    ArrayList students = manager.getStudents();

    for (int i = 0; i < students.size(); i++) {

      Student student = students.get(i);

      if (student.getName().equals(name)) {

        manager.removeStudent(student);

        System.out.println("Student removed");

        return;

      }

    }

    

    System.out.println("No such student found");

  }

  

  private void updateStudent() {

    System.out.print("Enter name: ");

    String name = scanner.next();

    

    ArrayList students = manager.getStudents();

    for (int i = 0; i < students.size(); i++) {

      Student student = students.get(i);

      if (student.getName().equals(name)) {

        System.out.print("Enter new name: ");

        String newName = scanner.next();

        System.out.print("Enter new age: ");

        int newAge = scanner.nextInt();

        System.out.print("Enter new gender: ");

        String newGender = scanner.next();

        System.out.print("Enter new major: ");

        String newMajor = scanner.next();

        

        manager.updateStudent(student, newName, newAge, newGender, newMajor);

        

        System.out.println("Student updated");

        return;

      }

    }

    

    System.out.println("No such student found");

  }

  

  private void showAllStudents() {

    ArrayList students = manager.getStudents();

    for (int i = 0; i < students.size(); i++) {

      Student student = students.get(i);

      System.out.println("Name: " + student.getName());

      System.out.println("Age: " + student.getAge());

      System.out.println("Gender: " + student.getGender());

      System.out.println("Major: " + student.getMajor());

      System.out.println();

    }

  }

}

这个类中,我们创建了一个带有菜单选项的简单界面,可以让用户动态增删改查学生信息。

综上所述,我学习Java编写学生管理系统的经验,希望对大家有所帮助。本文主要包括三个

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复