用JAVA编写一个学生类student,包含的属性有学号,姓名、性别、年龄等,将所有学生存储在一个数组中,编写学生管理程序实现如下操作: (1)增加一个学生; (2)根据学号删除某个学生; (3)将所有学生年龄增加一岁﹔ (4)按数组中顺序显示所有学生信息﹔ 将所有学生按姓名排序输出。
时间: 2023-05-28 22:07:41 浏览: 512
```
public class Student implements Comparable<Student>{
private int id;
private String name;
private String gender;
private int age;
public Student(int id, String name, String gender, int age) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
}
public int 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());
}
}
```
```
import java.util.Arrays;
import java.util.Scanner;
public class StudentManager {
private static Scanner sc = new Scanner(System.in);
private static Student[] students = new Student[10];
private static int size = 0;
public static void main(String[] args) {
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. 退出程序");
int choice = sc.nextInt();
switch (choice) {
case 1:
addStudent();
break;
case 2:
deleteStudent();
break;
case 3:
addOneYear();
break;
case 4:
showStudents();
break;
case 5:
sortStudents();
break;
case 0:
System.exit(0);
default:
System.out.println("无效选项,请重新选择");
break;
}
}
}
private static void addStudent() {
if (size == students.length) {
System.out.println("学生数组已满,无法继续添加");
return;
}
System.out.println("请输入学生信息:");
System.out.println("学号:");
int id = sc.nextInt();
System.out.println("姓名:");
String name = sc.next();
System.out.println("性别:");
String gender = sc.next();
System.out.println("年龄:");
int age = sc.nextInt();
students[size++] = new Student(id, name, gender, age);
System.out.println("学生添加成功");
}
private static void deleteStudent() {
System.out.println("请输入要删除的学生的学号:");
int id = sc.nextInt();
int index = -1;
for (int i = 0; i < size; i++) {
if (students[i].getId() == id) {
index = i;
break;
}
}
if (index == -1) {
System.out.println("学号为" + id + "的学生不存在");
return;
}
for (int i = index; i < size - 1; i++) {
students[i] = students[i + 1];
}
students[--size] = null;
System.out.println("学生删除成功");
}
private static void addOneYear() {
for (int i = 0; i < size; i++) {
students[i].setAge(students[i].getAge() + 1);
}
System.out.println("所有学生年龄增加一岁成功");
}
private static void showStudents() {
System.out.println("学生列表:");
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());
}
}
private static void sortStudents() {
Arrays.sort(students, 0, size);
System.out.println("按姓名排序后的学生列表:");
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());
}
}
}
```
阅读全文