利用java语言实现 3-5编写 一个学生类student,包含的属性有学号、姓名、性别、年龄等,将所有学生存储在一一个数组中,编写学生管理程序实现如下操作: (1)增加一个学生; (2)根据学号删除某个学生; (3)将所有学生年龄增加一岁; (4)按数组中顺序显示所有学生信息; (5)将所有学生按姓名排序输出。
时间: 2023-05-29 12:06:08 浏览: 104
java实验3.docx
```java
public class Student implements Comparable<Student> {
private String id; // 学号
private String name; // 姓名
private String gender; // 性别
private int age; // 年龄
public Student(String id, String name, String gender, int age) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//年龄增加一岁
public void increaseAge() {
this.age++;
}
//按照姓名排序
@Override
public int compareTo(Student o) {
return this.name.compareTo(o.getName());
}
@Override
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
'}';
}
}
```
```java
import java.util.Arrays;
import java.util.Scanner;
public class StudentManager {
private Student[] students; // 存储学生的数组
private int size; // 学生数量
public StudentManager(int capacity) {
students = new Student[capacity];
size = 0;
}
// 增加一个学生
public void addStudent(Student student) {
if (size < students.length) {
students[size++] = student;
System.out.println("增加成功!");
} else {
System.out.println("数组已满,增加失败!");
}
}
// 根据学号删除某个学生
public void deleteStudentById(String id) {
for (int i = 0; i < size; i++) {
if (students[i].getId().equals(id)) {
for (int j = i; j < size - 1; j++) {
students[j] = students[j + 1];
}
students[--size] = null;
System.out.println("删除成功!");
return;
}
}
System.out.println("没有找到该学号的学生,删除失败!");
}
// 将所有学生年龄增加一岁
public void increaseAge() {
for (int i = 0; i < size; i++) {
students[i].increaseAge();
}
System.out.println("年龄增加成功!");
}
// 按数组中顺序显示所有学生信息
public void showAllStudents() {
System.out.println("学生信息如下:");
for (int i = 0; i < size; i++) {
System.out.println(students[i]);
}
}
// 将所有学生按姓名排序输出
public void sortByName() {
Arrays.sort(students, 0, size);
System.out.println("按姓名排序后的学生信息如下:");
for (int i = 0; i < size; i++) {
System.out.println(students[i]);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StudentManager studentManager = new StudentManager(5);
while (true) {
System.out.println("请选择操作:");
System.out.println("1.增加一个学生");
System.out.println("2.根据学号删除某个学生");
System.out.println("3.将所有学生年龄增加一岁");
System.out.println("4.按数组中顺序显示所有学生信息");
System.out.println("5.将所有学生按姓名排序输出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("请输入学生信息(学号 姓名 性别 年龄):");
String id = scanner.next();
String name = scanner.next();
String gender = scanner.next();
int age = scanner.nextInt();
Student student = new Student(id, name, gender, age);
studentManager.addStudent(student);
break;
case 2:
System.out.println("请输入学号:");
String deleteId = scanner.next();
studentManager.deleteStudentById(deleteId);
break;
case 3:
studentManager.increaseAge();
break;
case 4:
studentManager.showAllStudents();
break;
case 5:
studentManager.sortByName();
break;
default:
System.out.println("输入错误,请重新选择!");
break;
}
}
}
}
```
阅读全文