使用java实现一下功能:编写一个学生类“java_3_26_3”,包含的属性有学号、姓名、性别、年龄,将所有学生存储在一个数组中,编写学生管理程序实现如下操作:①增加一个学生②根据学号删除某个学生③将所有学生年龄增加一岁④按数组中顺序显示所有学生信息⑤将所有学生按姓名排序
时间: 2023-05-29 18:03:39 浏览: 101
Java学生管理系统(控制台)实现了学生姓名,学号,居住地,年龄的添加,删除,修改,查询。解决了学号的重复问题,不存在的问题等
5星 · 资源好评率100%
```
public class java_3_26_3 {
private String id;
private String name;
private String gender;
private int age;
public java_3_26_3(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;
}
}
import java.util.Arrays;
import java.util.Scanner;
public class StudentManager {
private java_3_26_3[] students;
private int size;
public StudentManager(int capacity) {
students = new java_3_26_3[capacity];
size = 0;
}
public void addStudent(java_3_26_3 student) {
if (size == students.length) {
students = Arrays.copyOf(students, students.length * 2);
}
students[size++] = student;
}
public void deleteStudent(String id) {
int index = -1;
for (int i = 0; i < size; i++) {
if (students[i].getId().equals(id)) {
index = i;
break;
}
}
if (index != -1) {
for (int i = index; i < size - 1; i++) {
students[i] = students[i + 1];
}
students[--size] = null;
} else {
System.out.println("学号为" + id + "的学生不存在!");
}
}
public void increaseAge() {
for (int i = 0; i < size; i++) {
students[i].setAge(students[i].getAge() + 1);
}
}
public void showStudents() {
for (int i = 0; i < size; i++) {
System.out.println(students[i].getId() + "\t" + students[i].getName() + "\t" + students[i].getGender() + "\t" + students[i].getAge());
}
}
public void sortByName() {
Arrays.sort(students, 0, size, (o1, o2) -> o1.getName().compareTo(o2.getName()));
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StudentManager manager = new StudentManager(10);
while (true) {
System.out.println("请选择操作:1.增加学生 2.删除学生 3.增加年龄 4.显示学生信息 5.按姓名排序 0.退出");
int op = scanner.nextInt();
switch (op) {
case 1:
System.out.println("请输入学生信息(学号 姓名 性别 年龄):");
java_3_26_3 student = new java_3_26_3(scanner.next(), scanner.next(), scanner.next(), scanner.nextInt());
manager.addStudent(student);
break;
case 2:
System.out.println("请输入要删除的学生学号:");
manager.deleteStudent(scanner.next());
break;
case 3:
manager.increaseAge();
break;
case 4:
manager.showStudents();
break;
case 5:
manager.sortByName();
break;
case 0:
System.exit(0);
default:
System.out.println("输入有误,请重新输入!");
}
}
}
}
```
阅读全文