9.20
学生信息管理系统 基本的数组增删改查
import java.util.Scanner;
class student{
private String stunumber;
private String name;
private int age;
private boolean sex;
public double score;
public student() {
}
public student(String stunumber, String name, int age, boolean sex, double score) {
this.stunumber = stunumber;
this.name = name;
this.age = age;
this.sex = sex;
this.score = score;
}
public String getStunumber() {
return stunumber;
}
public void setStunumber(String stunumber) {
this.stunumber = stunumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public void display() {
System.out.println("学号: " + stunumber + " 姓名: " + name + " 年龄: " + age + " 性别: " + (sex ? "女" : "男") + " 成绩: " + score);
}
}
public class StudentManager {
private static student s1[] = new student[5];
private static int count = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("");
System.out.println(" 石家庄铁道大学软件工程系学生信息管理系统");
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、退出");
System.out.println("***************************************************************");
System.out.println("请输入选择:");
String choose = sc.next();
switch (choose) {
case "1":
showStudent();
break;
case "2":
System.out.print("请输入学号:");
String stunumber = sc.next();
System.out.print("请输入姓名:");
String name = sc.next();
System.out.print("请输入年龄:");
int age = sc.nextInt();
System.out.print("请输入性别(男生为false,女生为true):");
boolean sex = sc.nextBoolean();
System.out.print("请输入成绩:");
double score = sc.nextDouble();
addStudent (stunumber,name,age,sex,score);
break;
case "3":
System.out.print("请输入要删除的学号:");
stunumber = sc.next();
deleteStudent(stunumber);
break;
case "4":
System.out.print("请输入要修改的学号:");
stunumber = sc.next();
System.out.print("请输入新的姓名:");
name = sc.next();
System.out.print("请输入新的年龄:");
age = sc.nextInt();
System.out.print("请输入新的性别(男生输入false,女生输入true):");
sex = sc.nextBoolean();
System.out.print("请输入新的成绩:");
score = sc.nextDouble();
updateStudent (stunumber,name,age,sex,score);
break;
case "5":
System.out.print("请输入要查询的学号:");
stunumber = sc.next();
selectStudent(stunumber);
break;
case "6":
System.exit(0);
default:
System.out.println("没有这个选项");
break;
}
}
}
public static void showStudent(){
if(count == 0){
System.out.println("没有学生信息");
}
for (int i = 0; i < count; i++) {
s1[i].display();
}
}
public static void addStudent (String stunumber,String name,int age,boolean sex,double score){
for (int i = 0; i < count; i++) {
if (s1[i].getStunumber().equals(stunumber)) {
System.out.println("学号已存在");
return;
}
}
if (count < 5) {
s1[count++] = new student(stunumber, name, age, sex, score);
System.out.println("添加成功");
} else {
System.out.println("学生信息已满,无法添加新学生");
}
}
public static void deleteStudent(String stunumber){
for (int i = 0; i < count; i++) {
if (s1[i].getStunumber().equals(stunumber)) {
s1[i] = null;
System.out.println("删除成功");
for (int j = i; j < count - 1; j++) {
s1[j] = s1[j + 1];
}
s1[--count] = null;
return;
}
}
System.out.println("未找到该学号的学生信息");
}
public static void updateStudent (String stunumber,String name,int age,boolean sex,double score){
for (int i = 0; i < count; i++) {
if (s1[i].getStunumber().equals(stunumber)) {
s1[i].setName(name);
s1[i].setAge(age);
s1[i].setSex(sex);
s1[i].setScore(score);
System.out.println("修改成功");
return;
}
}
System.out.println("未找到该学号的学生信息");
}
public static void selectStudent(String stunumber){
for (int i = 0; i < count; i++) {
if (s1[i].getStunumber().equals(stunumber)) {
s1[i].display();
return;
}
}
System.out.println("未找到该学号的学生信息");
}
}