本文实例为大家分享了java学生成绩管理系统,供大家参考,具体内容如下
要求:
完善Student类,Student类包含学号、姓名、年级、专业、三门功课的成绩(英语、高数、计算机)的成员变量,完善成绩录入方法、设计按学号查找方法、按姓名查找方法、按单科成绩排序的方法。
设计主类,实例化包含5个学生信息的学生数组,查找某一个学生的信息并打印出来,同时打印这5个学生按某一科成绩的按高到低的排序信息(学号、姓名、成绩);输出所有学生的三门单科平均成绩。
首先先创建一个student类
使用构造方法来初始化
学号、姓名、年级、专业、三门功课的成绩
先打包
在分类
student类
- package swpu.student;
- public class Student {
- public String number;
- public String name;
- public String major;
- public int math;
- public int computer;
- public int english;
- public int total;
- //对象数组初始化,使用构造方法
- public Student(String newname,String nmajor,String newnumber,int nmath,int ncom,int ne){
- number = newnumber;
- major =nmajor;
- name = newname;
- math = nmath;
- computer = ncom;
- english = ne;
- }
- public String getMajor() {
- return major;
- }
- public void setMajor(String major) {
- this.major = major;
- }
- public int getEnglish() {
- return english;
- }
- public void setEnglish(int english) {
- this.english = english;
- }
- public String getNumber() {
- return number;
- }
- public void setNumber(String number) {
- this.number = number;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getMath() {
- return math;
- }
- public void setMath(int math) {
- this.math = math;
- }
- public int getComputer() {
- return computer;
- }
- public void setComputer(int computer) {
- this.computer = computer;
- }
- }
排序类
rank类
- package swpu.student;
- public class Rank {
- public static void rankscore(Student [] arr,int n){
- //数学
- if(n==1) {
- for (int i = 0; i < arr.length-1; i++) {
- int index = i;
- int j;
- // 找出最小值得元素下标
- for (j = i + 1; j < arr.length; j++) {
- if (arr[j].math > arr[index].math) {
- index = j;
- }
- }
- int tmp = arr[index].math;
- arr[index].math = arr[i].math;
- arr[i].math = tmp;
- }
- }
- //英语
- if(n==2) {
- for (int i = 0; i < arr.length-1; i++) {
- int index = i;
- int j;
- // 找出最小值得元素下标
- for (j = i + 1; j < arr.length; j++) {
- if (arr[j].english > arr[index].english) {
- index = j;
- }
- }
- int tmp = arr[index].english;
- arr[index].english = arr[i].english;
- arr[i].english = tmp;
- }
- }
- //计算机
- if(n==3) {
- for (int i = 0; i < arr.length-1; i++) {
- int index = i;
- int j;
- // 找出最小值得元素下标
- for (j = i + 1; j < arr.length; j++) {
- if (arr[j].computer > arr[index].computer) {
- index = j;
- }
- }
- int tmp = arr[index].computer;
- arr[index].computer = arr[i].computer;
- arr[i].computer = tmp;
- }
- }
- }
- }
这里使用了静态方法传入成绩的值
查找类
search类
- package swpu.student;
- public class Search {
- //书写两种方法(学号,姓名)
- public int StuNum(Student arr[],String y)//传入数组,查找值,使用字符串的比较
- {
- for(int i = 0;i<arr.length;i++)
- {
- if(arr[i].number.equals(y))
- return i;
- }
- return -1;
- }
- public int StuNam(Student stu[],String x) {
- for(int i = 0;i<stu.length;i++)
- {
- if(stu[i].name.equals(x))
- return i;
- }
- return -1;
- }
- }
主要类
Instudent类
- package swpu.student;
- import java.util.Scanner;
- public class Instudent {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner in = new Scanner(System.in);
- Student []stu = new Student[5];
- //学生成绩初始化
- stu[0] = new Student("Jack","软工 ","20183101",80,90,85);
- stu[1] = new Student("Rose","大数据","20183102",99,93,90);
- stu[2] = new Student("John","网安全","20183103",87,70,74);
- stu[3] = new Student("Andi","网工程","20183104",67,66,68);
- stu[4] = new Student("Mike","物联网","20183105",56,55);
- //局部变量的初始化
- String nu1 = "";
- String na1 = "";
- String ma1 = "";
- int t1=0,t2=0,t3=0;
- System.out.println("-------------------学生成绩管理系统------------------------");
- //输入学生信息
- for(int i=0;i<stu.length;i++) {
- System.out.println("请输入第"+(i+1)+"个学生的姓名,专业,学号,数学成绩,计算机成绩,英语成绩");
- na1 = in.next();//姓名
- ma1 = in.next();//专业
- nu1 = in.next();//学号
- t1 = in.nextInt();
- t2 = in.nextInt();
- t3 = in.nextInt();
- stu[i].setNumber(nu1);
- stu[i].setName(na1);
- stu[i].setMajor(ma1);
- stu[i].setEnglish(t3);
- stu[i].setComputer(t2);
- stu[i].setMath(t2);
- }
- Search search = new Search();
- //选择需要的查找的方法
- System.out.println("选择需要的查找的方法,1学号,2姓名");
- int p = in.nextInt();
- if(p==1) {
- //使用学号的方法进行查找
- System.out.println("输入您所需要查找的学生学号");
- String y = in.next();
- int x = search.StuNum(stu,y);
- if(x>=0)
- System.out.println("学号:"+stu[x].number+" 学生:"+stu[x].name+" 专业:"+stu[x].major+" 数学:"+stu[x].math+" 计算机:"+stu[x].computer+" 英语:"+stu[x].english);
- else
- System.out.println("输入的学生不存在");
- }
- if(p==2) {
- //使用姓名的方法进行查找
- System.out.println("输入您所需要查找的学生姓名");
- String thename = in.next();
- int w = search.StuNam(stu,thename);
- if(w>=0)
- System.out.println("学号:"+stu[w].number+" 学生:"+stu[w].name+" 专业:"+stu[w].major+" 数学:"+stu[w].math+" 计算机:"+stu[w].computer+" 英语:"+stu[w].english);
- else
- System.out.println("输入的学生不存在");
- }
- System.out.println("是否需要对单科成绩进行排名 [Y/N] 1 =yes,2=no");
- int op = in.nextInt();
- if(op==1) {
- //单科成绩的排序(输入所需要科目然后直接进行排序)
- Rank rank = new Rank();//创建对象
- System.out.println("输入所需要排序的成绩编号,1:数学,2:英语,3:计算机");
- int major = in.nextInt();
- rank.rankscore(stu,major);
- //输出排序后的成绩
- for(int i = 0;i < stu.length;i++) {
- System.out.println("学号:"+stu[i].number+" 学生:"+stu[i].name+" 专业:"+stu[i].major+" 数学:"+stu[i].math+" 计算机:"+stu[i].computer+" 英语:"+stu[i].english);
- }
- }
- else {
- System.out.println("结束,退出系统");
- }
- }
- }
其中使用构造方法初始化的时已经存入了值,因此在使用set方法输入学生信息时其实是修改学生信息,在构造方法初始化的时候可以不用那么复杂 可直接根据数据类型 例如:
- stu[0] = new Student(" "," ",0);
- stu[1] = new Student(" ",0);
- stu[2] = new Student(" ",0);
- stu[3] = new Student(" ",0);
- stu[4] = new Student(" ",0);
注意 在声明局部变量的时候一定要记住初始化,否则将值传入数组的时候会出现报错
运行截图: