先创建一个Student类
public class Student {
private int number;//学号
private String name;//姓名
private String department;//院系
private String grade;//班级
private double score;//分数//需要什么信息就自行添加
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public Student() {
}
public Student(int number, String name, String department, String grade, double score) {
super();
this.number = number;
this.name = name;
this.department = department;
this.grade = grade;
this.score = score;
}
}再创建一个StudentSystemTest类
import java.util.ArrayList;
import java.util.Scanner;public class StudentSystemTest {
public static void main(String[] args) {
System.out.println(" \t 欢迎使用学生信息管理系统 \t ");
System.out.println("***********************************");
System.out.println("\t1、添加学生信息");
System.out.println("\t2、删除学生信息");
System.out.println("\t3、修改学生信息");
System.out.println("\t4、浏览学生信息");
System.out.println("\t5、查询学生信息");
System.out.println("\t6、学生成绩排名");
System.out.println("请选择1-6进行操作:\t");
//Student[] student = new Student[100];
ArrayList<Student> array = new ArrayList<>();
Student stud1 = new Student(1,"\t小明","\t信工","\t计科211\t",100);
Student stud2 = new Student(2,"\t小王","\t信工","\t计科211\t",96);
Student stud3 = new Student(3,"\t小张","\t信工","\t计科211\t",100);
Student stud4 = new Student(4,"\t小红","\t信工","\t计科211\t",80);
array.add(stud1);
array.add(stud2);
array.add(stud3);
array.add(stud4);
while(true) {
Scanner scanner = new Scanner(System.in);
int choice=scanner.nextInt();
switch(choice) {
case 1://添加学生信息
AddStudent(array);
break;
case 2://删除学生信息
RemoveStudent(array);
break;
case 3://修改学生信息
ReviseStudent(array);
break;
case 4://浏览学生信息
SearchStudent(array);
break;
case 5://查询学生成绩
searchStudent(array);
break;
case 6://学生成绩排名
RankStudent(array);
break;
default:
System.out.println("系统无法识别该操作,请重新选择!");
break;
}
}
}
//添加学生信息
public static void AddStudent(ArrayList<Student> array) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生的学号:");
int number = scanner.nextInt();
boolean isFlag = true;
//判断输入的学号和已存在的学号是否相同
for(int i=0;i<array.size();i++) {
Student s =array.get(i);
if(s.getNumber()==number) {
isFlag = false;
System.out.println("该学号已存在,请重新输入!");
break;
}
}
if(isFlag) {
Scanner scanners = new Scanner(System.in);
System.out.println("请输入学生的姓名:");
String Name = scanners.nextLine();
System.out.println("请输入学生的学院:");
String department = scanners.nextLine();
System.out.println("请输入学生的班级:");
String grade = scanners.nextLine();
System.out.println("请输入学生的分数:");
int score = scanners.nextInt();
Student s=new Student();
s.setNumber(number);
s.setName(Name);
s.setDepartment(department);
s.setGrade(grade);
s.setScore(score);
array.add(s);
System.out.println("添加成功!");
}
}
//删除学生信息
public static void RemoveStudent(ArrayList<Student> array) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您要删除的学生的学号: ");
int Number = scanner.nextInt();int index = -1;
//判断是否学生信息是否存在
for (int i = 0; i < array.size(); i++) {
Student s = array.get(i);
if (s.getNumber()==Number) {
index = i;
break;
}
}if (index == -1) {
System.out.println("未找到相关学生信息,请重新选择! ");
} else {
array.remove(index); // 删除获取的学生信息
System.out.println("删除学生信息成功!");
}}
//修改学生信息
public static void ReviseStudent(ArrayList<Student> array) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要修改的学生的学号:");
int number = scanner.nextInt();
for(int i=0;i<array.size();i++) {
Student s = array.get(i);
//判断是否学生信息是否存在
if(s.getNumber()==number) {
break;
}
}
if(false) {
System.out.println("该信息不存在,请重新输入!");
}else {
System.out.println("请输入学生的姓名:");
String name = scanner.nextLine();
System.out.println("请输入学生的学院:");
String department = scanner.nextLine();
System.out.println("请输入学生的班级:");
String grade = scanner.nextLine();
System.out.println("请输入学生的分数:");
int score = scanner.nextInt();
Student s=new Student();
s.setNumber(number);
s.setName(name);
s.setDepartment(department);
s.setGrade(grade);
s.setScore(score);
array.add(s);
System.out.println("信息修改成功!");
}
}
//浏览学生信息
public static void SearchStudent(ArrayList<Student> array) {
System.out.println("\t浏览学生信息");
System.out.println("信息如下:");
for(int i=0;i<array.size();i++) {
Student S = array.get(i);
System.out.println("学号:\t姓名:\t学院:\t班级:\t分数:");
System.out.println(S.getNumber()+"\t"+S.getName()+"\t"+S.getDepartment()+"\t"+S.getGrade()+"\t"+S.getScore());
}
}
//查询学生成绩
public static void searchStudent(ArrayList<Student> array) {
System.out.println("\t查询学生成绩");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入需要查询的学生的学号:");
int number = scanner.nextInt();
for(int i=0;i<array.size();i++) {
Student S = array.get(i);
if(S.getNumber()==number) {
System.out.println("学号:\t姓名:\t学院:\t班级:\t分数:");
System.out.println(S.getNumber()+"\t"+S.getName()+"\t"+S.getDepartment()+"\t"+S.getGrade()+"\t"+S.getScore());
}
}
}
//学生成绩排名
public static void RankStudent(ArrayList<Student> array) {
System.out.println("\t学生成绩排名");//创建一个Student[]
Student[] student = new Student[1000];
for(int i =0;i<array.size();i++) {
for(int j=0;j<array.size()-j;j++) {
if(student[j].getScore()<student[j+1].getScore()) {
Student temp = student[j];
student[j] = student[j+1];
student[j+1] = temp;
}
System.out.println(student[i]);
}
}
}}
Java学生信息管理系统
最新推荐文章于 2024-09-10 10:21:38 发布