如何使用Java代码实现小型学生信息管理程序(不用实现界面),功能包括基本的添加学生、删除、修改和查询学生信息等操作,要求用链表实现:用户可选择五个操作之一:添加学生、修改学生、删除学生、查询学生、显示学生链表的当前状态和退出程序,测试上述所有功能。
时间: 2024-01-27 08:05:02 浏览: 51
好的,以下是一份使用Java代码实现小型学生信息管理程序的参考:
```java
import java.util.Scanner;
class Student {
private String name;
private int id;
private int age;
public Student(String name, int id, int age) {
this.name = name;
this.id = id;
this.age = age;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setId(int id) {
this.id = id;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", id=" + id +
", age=" + age +
'}';
}
}
class Node {
private Student data;
private Node next;
public Node(Student data) {
this.data = data;
this.next = null;
}
public Student getData() {
return data;
}
public void setData(Student data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
class LinkedList {
private Node head;
public LinkedList() {
head = null;
}
public void add(Student data) {
Node node = new Node(data);
if (head == null) {
head = node;
} else {
Node current = head;
while (current.getNext() != null) {
current = current.getNext();
}
current.setNext(node);
}
}
public void delete(int id) {
if (head != null) {
if (head.getData().getId() == id) {
head = head.getNext();
} else {
Node current = head;
while (current.getNext() != null && current.getNext().getData().getId() != id) {
current = current.getNext();
}
if (current.getNext() != null) {
current.setNext(current.getNext().getNext());
}
}
}
}
public void update(Student data) {
if (head != null) {
Node current = head;
while (current != null && current.getData().getId() != data.getId()) {
current = current.getNext();
}
if (current != null) {
current.setData(data);
}
}
}
public Student search(int id) {
Node current = head;
while (current != null && current.getData().getId() != id) {
current = current.getNext();
}
if (current != null) {
return current.getData();
} else {
return null;
}
}
public void display() {
Node current = head;
while (current != null) {
System.out.println(current.getData().toString());
current = current.getNext();
}
}
}
public class StudentManagement {
public static void main(String[] args) {
LinkedList list = new LinkedList();
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("6. 退出程序");
int option = scanner.nextInt();
switch (option) {
case 1:
System.out.println("请输入学生姓名:");
String name = scanner.next();
System.out.println("请输入学生学号:");
int id = scanner.nextInt();
System.out.println("请输入学生年龄:");
int age = scanner.nextInt();
Student student = new Student(name, id, age);
list.add(student);
System.out.println("添加成功!");
break;
case 2:
System.out.println("请输入要修改的学生学号:");
int updateId = scanner.nextInt();
Student updateStudent = list.search(updateId);
if (updateStudent != null) {
System.out.println("请输入要修改的学生姓名:");
String updateName = scanner.next();
System.out.println("请输入要修改的学生年龄:");
int updateAge = scanner.nextInt();
Student newStudent = new Student(updateName, updateId, updateAge);
list.update(newStudent);
System.out.println("修改成功!");
} else {
System.out.println("未找到该学生!");
}
break;
case 3:
System.out.println("请输入要删除的学生学号:");
int deleteId = scanner.nextInt();
list.delete(deleteId);
System.out.println("删除成功!");
break;
case 4:
System.out.println("请输入要查询的学生学号:");
int searchId = scanner.nextInt();
Student searchStudent = list.search(searchId);
if (searchStudent != null) {
System.out.println(searchStudent.toString());
} else {
System.out.println("未找到该学生!");
}
break;
case 5:
list.display();
break;
case 6:
System.out.println("程序已退出!");
System.exit(0);
break;
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
}
```
在上述代码中,我们首先定义了学生类 `Student` 和节点类 `Node`,然后定义了链表类 `LinkedList`,其中包括添加学生、删除学生、修改学生、查询学生、显示学生链表的当前状态等操作。最后,在 `main` 方法中通过 `Scanner` 类实现了基本的用户交互和测试。
希望能对你有所帮助!
阅读全文