定义数组存储3个学生对象。
学生的属性:学号,姓名,年龄。
要求1:添加的时候需要进行学号的唯一性判断。
要求2:添加完毕之后,遍历所有学生信息。
要求3:通过id删除学生信息
如果存在,则删除,如果不存在,则提示删除失败。
要求4:删除完毕之后,遍历所有学生信息。
要求5:id为2的学生,年龄+1岁
package wwww;
import java.util.Scanner;
public class Student {
private int id;
private String name;
private int age;
public Student() {
}
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
// 构造函数、getter和setter方法省略
public static boolean checkUnique(Student[] students, int id) {
for (Student student : students) {
if (student != null && student.getId() == id) {
return false;
}
}
return true;
}
public static Student[] addStudent(Student[] students, Student newStudent) {
if (checkUnique(students, newStudent.getId())) {
Student[] newStudents = new Student[students.length + 1];
System.arraycopy(students, 0, newStudents, 0, students.length);
newStudents[students.length] = newStudent;
return newStudents;
} else {
return students;
}
}
public static void printAllStudents(Student[] students) {
for (Student student : students) {
if (student != null) {
System.out.println(student.toString());
}
}
}
public static Student[] deleteStudent(Student[] students, int id) {
if (!checkUnique(students, id)) {
Student[] newStudents = new Student[students.length - 1];
int newIndex = 0;
for (Student student : students) {
if (student != null && student.getId() != id) {
newStudents[newIndex] = student;
newIndex++;
}
}
return newStudents;
} else {
System.out.println("删除失败,学号不存在。");
return students;
}
}
public static void increaseAgeById(Student[] students, int id) {
for (Student student : students) {
if (student != null && student.getId() == id) {
student.setAge(student.getAge() + 1);
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Student[] students = new Student[0];
students = addStudent(students, new Student(1, "Alice", 20));
students = addStudent(students, new Student(2, "Bob", 21));
students = addStudent(students, new Student(3, "Charlie", 19));
System.out.println("请输入学生信息:");
System.out.print("学号:");
int id = scanner.nextInt();
System.out.print("姓名:");
String name = scanner.next();
System.out.print("年龄:");
int age = scanner.nextInt();
System.out.println("添加完毕后的学生信息:");
printAllStudents(students);
increaseAgeById(students, 2);
System.out.println("年龄增加后的学生信息:");
printAllStudents(students);
students = deleteStudent(students, 2);
System.out.println("删除完毕后的学生信息:");
printAllStudents(students);
students = addStudent(students, new Student(id, name, age));
System.out.println("重新添加的学生信息:");
printAllStudents(students);
}
/**
* 获取
* @return id
*/
public int getId() {
return id;
}
/**
* 设置
* @param id
*/
public void setId(int id) {
this.id = id;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "Student{id = " + id + ", name = " + name + ", age = " + age + "}";
}
}