java二维数组学生系统
时间: 2023-09-28 11:04:11 浏览: 79
下面是一个简单的Java二维数组学生系统,该系统可以实现添加、删除、修改和查询学生信息。
```java
import java.util.Scanner;
public class StudentSystem {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[][] students = new String[100][3]; // 二维数组存储学生信息,每个学生有三个属性:姓名、年龄、性别
int count = 0; // 记录学生数量
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. 退出系统");
int choice = input.nextInt();
switch (choice) {
case 1:
System.out.print("请输入学生姓名:");
String name = input.next();
System.out.print("请输入学生年龄:");
String age = input.next();
System.out.print("请输入学生性别:");
String gender = input.next();
students[count][0] = name;
students[count][1] = age;
students[count][2] = gender;
System.out.println("添加学生成功!");
count++;
break;
case 2:
System.out.print("请输入要删除的学生姓名:");
String deleteName = input.next();
int deleteIndex = -1; // 记录要删除的学生在数组中的索引
for (int i = 0; i < count; i++) {
if (deleteName.equals(students[i][0])) {
deleteIndex = i;
break;
}
}
if (deleteIndex == -1) {
System.out.println("没有找到该学生!");
} else {
for (int i = deleteIndex; i < count - 1; i++) {
students[i] = students[i + 1];
}
count--;
System.out.println("删除学生成功!");
}
break;
case 3:
System.out.print("请输入要修改的学生姓名:");
String modifyName = input.next();
int modifyIndex = -1; // 记录要修改的学生在数组中的索引
for (int i = 0; i < count; i++) {
if (modifyName.equals(students[i][0])) {
modifyIndex = i;
break;
}
}
if (modifyIndex == -1) {
System.out.println("没有找到该学生!");
} else {
System.out.print("请输入新的学生姓名:");
String newName = input.next();
System.out.print("请输入新的学生年龄:");
String newAge = input.next();
System.out.print("请输入新的学生性别:");
String newGender = input.next();
students[modifyIndex][0] = newName;
students[modifyIndex][1] = newAge;
students[modifyIndex][2] = newGender;
System.out.println("修改学生成功!");
}
break;
case 4:
System.out.print("请输入要查询的学生姓名:");
String searchName = input.next();
boolean found = false; // 标记是否找到该学生
for (int i = 0; i < count; i++) {
if (searchName.equals(students[i][0])) {
System.out.println("姓名:" + students[i][0] + ",年龄:" + students[i][1] + ",性别:" + students[i][2]);
found = true;
break;
}
}
if (!found) {
System.out.println("没有找到该学生!");
}
break;
case 5:
System.out.println("退出系统!");
System.exit(0);
break;
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
}
```
阅读全文