用java实现编写 一个学生类student,包含的属性有学号、姓名、性别、年龄等,将所有学生存储在一一个数组中,编写学生管理程序实现如下操作: (1)增加一个学生; (2)根据学号删除某个学生; (3)将所有学生年龄增加一岁; (4)按数组中顺序显示所有学生信息; (5)将所有学生按姓名排序输出。
时间: 2023-05-27 21:06:23 浏览: 255
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 String getName() {
return name;
}
public String getGender() {
return gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int compareTo(Student o) {
return name.compareTo(o.getName());
}
@Override
public String toString() {
return "学号:" + id + "\t姓名:" + name + "\t性别:" + gender + "\t年龄:" + age;
}
}
```
```java
import java.util.Arrays;
import java.util.Scanner;
public class StudentManager {
private static final int MAX_SIZE = 100; // 学生数组最大容量
private Student[] students; // 学生数组
private int size; // 学生数组中已有学生的数量
public StudentManager() {
students = new Student[MAX_SIZE];
size = 0;
}
/**
* 增加一个学生
*/
public void addStudent() {
if (size >= MAX_SIZE) {
System.out.println("学生数量已达到最大值,无法添加!");
return;
}
Scanner scanner = new Scanner(System.in);
System.out.print("请输入学号:");
String id = scanner.next();
System.out.print("请输入姓名:");
String name = scanner.next();
System.out.print("请输入性别:");
String gender = scanner.next();
System.out.print("请输入年龄:");
int age = scanner.nextInt();
Student student = new Student(id, name, gender, age);
students[size] = student;
size++;
System.out.println("添加成功!");
}
/**
* 根据学号删除某个学生
*/
public void deleteStudent() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要删除的学生的学号:");
String id = scanner.next();
int index = -1;
for (int i = 0; i < size; i++) {
if (students[i].getId().equals(id)) {
index = i;
break;
}
}
if (index == -1) {
System.out.println("没有找到该学生!");
return;
}
for (int i = index; i < size - 1; i++) {
students[i] = students[i + 1];
}
size--;
System.out.println("删除成功!");
}
/**
* 将所有学生年龄增加一岁
*/
public void addAge() {
for (int i = 0; i < size; i++) {
students[i].setAge(students[i].getAge() + 1);
}
System.out.println("所有学生年龄增加成功!");
}
/**
* 按数组中顺序显示所有学生信息
*/
public void showStudents() {
if (size == 0) {
System.out.println("当前没有学生!");
return;
}
for (int i = 0; i < size; i++) {
System.out.println(students[i]);
}
}
/**
* 将所有学生按姓名排序输出
*/
public void sortStudents() {
if (size == 0) {
System.out.println("当前没有学生!");
return;
}
Arrays.sort(students, 0, size);
System.out.println("按姓名排序输出所有学生信息:");
for (int i = 0; i < size; i++) {
System.out.println(students[i]);
}
}
}
```
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
StudentManager manager = new StudentManager();
Scanner scanner = new Scanner(System.in);
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. 将所有学生按姓名排序输出");
System.out.println("0. 退出系统");
System.out.println("**************************************");
System.out.print("请输入操作编号:");
int choice = scanner.nextInt();
switch (choice) {
case 0:
System.out.println("感谢使用学生管理系统,再见!");
return;
case 1:
manager.addStudent();
break;
case 2:
manager.deleteStudent();
break;
case 3:
manager.addAge();
break;
case 4:
manager.showStudents();
break;
case 5:
manager.sortStudents();
break;
default:
System.out.println("无效操作,请重新输入!");
break;
}
}
}
}
```
阅读全文