//StudentMain.java
public class StudentMain {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
System.out.print("请输入学生总人数");
int count= scn.nextInt();
Student [] students=new Student [count];
while (true) {
System.out.println("1.新增一名学生 2.根据学号查询学生 3.根据学号修改学生 4. 根据学号删除一名学生 5. 查看所有学生 其他.退出");
int x= scn.nextInt();
switch(x) {
case 1 : {
System.out.println("请输入学生编号");
String stuid=scn.next();
System.out.println("请输入学生姓名");
String name=scn.next();
System.out.println("请输入该学生语文成绩");
int chinese= scn.nextInt();
System.out.println("请输入该学生数学成绩");
int math= scn.nextInt();
System.out.println("请输入该学生英语成绩");
int english= scn.nextInt();
Student student =new Student(stuid, name, chinese, math, english);
students=StudentAll.save(student, students);
break;
}
case 2:{
System.out.println("请输入学生编号");
String stuid=scn.next();
System.out.println(StudentAll.getStudent(stuid, students));
break;
}
case 3:{
System.out.println("请输入要修改的学生编号");
String stuid=scn.next();
System.out.println("请输入学生姓名");
String name=scn.next();
System.out.println("请输入该学生语文成绩");
int chinese= scn.nextInt();
System.out.println("请输入该学生数学成绩");
int math= scn.nextInt();
System.out.println("请输入该学生英语成绩");
int english= scn.nextInt();
Student student =new Student(stuid, name, chinese, math, english);
students=StudentAll.update(stuid, student, students);
break;
}
case 4:{
System.out.println("请输入学生编号");
String stuid=scn.next();
students=StudentAll.delStudent(stuid, students);
break;
}
case 5:{
StudentAll.getStudeltAll(students);
break;
}default:{
System.exit(0);
}
}
}
}
}
//Student.java
public class Student {
private String stuid;
private String name;
private int chinese;
private int math;
private int english;
private int avg;
private int sum;
public int getAvg() {
return avg;
}
public void setAvg(int avg) {
this.avg = avg;
}
public int getSum() {
return sum;
}
public void setSum(int sum) {
this.sum = sum;
}
public String getStuid() {
return stuid;
}
public void setStuid(String stuid) {
this.stuid = stuid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
public String getMessage() {
String messages="学生编号 :"+this.stuid+" 学生姓名: "+this.name+" 语文:"+this.chinese+" 数学:"+this.math+" 英语: "+this.english+" 总分 :"+this.sum+" 平均分 :"+this.avg;
return messages;
}
public Student(String stuid,String name,int chinese,int math, int english) {
this.chinese=chinese;
this.english=english;
this.math=math;
this.name=name;
this.stuid=stuid;
this.sum=chinese+math+english;
this.avg=this.sum/3;
}
public Student() {
}
}
//StudentAll.java
public class StudentAll {
public static Student[] save(Student student,Student [] students) {
boolean fal=checkSid(student.getStuid(), students);//先查询是否有这个 学生编号
if(fal) {
System.out.println("该学生编号已存在,请重新输入");
return students;
}
for(int i=0;i<students.length;i++) {
if(students[i]==null) {
students[i]=student;
break;
}
}
return students;
}
public static String getStudent(String stuid,Student [] students) {
Student s=null;
for (Student student : students) {
if(student!=null&&student.getStuid().equals(stuid)) {
s=student;
}
}
String messages=s==null?"error: 该编号不存在请重新输入":s.getMessage();
return messages;
}
public static Student[] update (String stuid,Student student,Student [] students) {
boolean fal=checkSid(stuid, students);//先查询是否有这个 学生编号
if(!fal) {
System.out.println("请输入正确的学生编号");
return students;
}
for(int i=0;i<students.length;i++) {
if(students[i]!=null&&students[i].getStuid().equals(stuid)) {
students[i]=student;
}
}
return students;
}
public static Student[] delStudent(String stuid,Student [] students) {
boolean fal=checkSid(stuid, students);//先查询是否有这个 学生编号
if(!fal) {
System.out.println("请输入正确的学生编号");
return students;
}
for(int i=0;i<students.length;i++) {
if(students[i]!=null&&students[i].getStuid().equals(stuid)) {
students[i]=null;
}
}
return students;
}
public static void getStudeltAll(Student [] students) {
for (Student student : students) {
if(student!=null) {
System.out.println(student.getMessage());
}
}
}
//检验学生编号是否存在方法
public static boolean checkSid(String stuid,Student [] students) {
boolean fal=false;
String messages=getStudent(stuid, students);//先查询是否有这个 学生编号
if(!messages.contains("error")) {//判断是否包含 error 有则没有此学生编号
fal=true;
}
return fal;
}
}