java 学生管理系统

Posted 张凯::get("新技能")

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 学生管理系统相关的知识,希望对你有一定的参考价值。

//student.java
 
package com.sran.www;
 
import java.util.Arrays;
import java.util.Scanner;
 
public class Student implements Comparable<Student>{
    int num;
    String name;
    int age;
    double[]s;
    double sum; //存储学生的总分
     
    public Student()
    {
        s=new double[3];
    }
 
    public static boolean insert()
    {
        Scanner sc=new Scanner(System.in);
        String str=null;
        boolean be=true;
         
        do
        {
            Student stud=new Student();
            System.out.println("开始录入学生的相关信息.....");
            System.out.print("学号:");
            stud.num=sc.nextInt();
            System.out.print("姓名:");
            stud.name=sc.next();
            System.out.print("年龄:");
            stud.age=sc.nextInt();
             
            System.out.println("依次输入三门课的成绩:");
            System.out.print("语文:");
            stud.s[0]=sc.nextDouble();
            System.out.print("数学:");
            stud.s[1]=sc.nextDouble();
            System.out.print("英语:");
            stud.s[2]=sc.nextDouble();
             
            stud.sum=stud.calculate_sum();
                 
            StudentTest.set.add(stud);
             
            System.out.println("如果继续录入,则输入%,否则输入 #");
            System.out.print("请输入:");
            str=sc.next();
             
            if(!(str.equals("%")||str.equals("#")))
            {
                be=false;
                System.out.println("你输入有误,已强制结束录入操作");
            }
             
        }while(str.equals("%") && be);
         
        return be;
    }
     
    public static boolean delete()
    {
        String str=null;
        boolean be=true;
        System.out.println("开始删除学生的相关信息.....");
        do
        {
            System.out.print("请输入要删除的学生的学号:");
            Scanner sc=new Scanner(System.in);
            int s=sc.nextInt();
             
            boolean flag=false;  //设一标志位
             
            for(Student stud:StudentTest.set)
            {
                if(s==stud.num)
                {
                    StudentTest.set.remove(stud);
                    flag=true;
                    System.out.println("已删除该学生的信息");
                    break;
                }
             
            }
             
            if(flag==false)
            {
                System.out.println("你要删除的学生的学号不存在");
            }
             
            System.out.println("如果继续删除,则输入 %,否则输入#");
            System.out.print("请输入:");
            str=sc.next();
             
            if(!(str.equals("%")||str.equals("#")))
            {
                be=false;
                System.out.println("你输入有误,已强制结束删除操作");
            }
             
        }while(str.equals("%") && be);
         
        return be;
    }
     
     
    public static boolean seek()
    {
        String str=null;
        boolean be=true;
        System.out.println("开始查找学生的相关信息.....");
        do
        {
            System.out.print("请输入要查找的学生的学号:");
            Scanner sc=new Scanner(System.in);
            int s=sc.nextInt();
             
            boolean flag=false;
             
            for(Student stud:StudentTest.set)
            {
                if(s==stud.num)
                {
                    flag=true;  //修改标志位
                    System.out.println("你要查找的学生的信息:");
                     
                    System.out.println("学号:"+stud.num);
                    System.out.println("姓名:"+stud.name);
                    System.out.println("年龄:"+stud.age);
                    System.out.println("语文成绩:"+stud.s[0]);
                    System.out.println("数学成绩:"+stud.s[1]);
                    System.out.println("英语成绩:"+stud.s[2]);
                     
                    break;
                }
            }
             
            if(flag==false)
            {
                System.out.println("你要查找的学生的学号不存在");
            }
             
             
            System.out.println("如果继续查找,则输入 %,否则输入#");
            System.out.print("请输入:");
            str=sc.next();
             
            if(!(str.equals("%")||str.equals("#")))
            {
                be=false;
                System.out.println("你输入的有误,已强制结束查询操作");
            }
             
        }while(str.equals("%") && be);
             
        return be;
    }
     
    public double calculate_sum()
    {  
        sum=s[0]+s[1]+s[2];
        return sum;
    }
 
    @Override
    public int compareTo(Student stud) {
        this.sum=this.calculate_sum();
        stud.sum=stud.calculate_sum();
         
        if(this.sum>stud.sum) return -1;
        else if(this.sum<stud.sum) return 1;
        else
        {
            if(this.name.compareTo(stud.name)>0)  return -1;
            else if(this.name.compareTo(stud.name)<0)  return 1;
            else
                return 0;
        }
    }
     
    public String toString()
    {
        System.out.println("学号"+"\t\t"+"姓名"+"\t\t"+"年龄"+"\t\t"+"语文"+"\t\t"+"数学"+"\t\t"+"英语"+"\t\t"+"总分");
        return(num+"\t\t"+name+"\t\t"+age+"\t\t"+s[0]+"\t\t"+s[1]+"\t\t"+s[2]+"\t\t"+sum);
     
    }
     
    public static boolean update()
    {
        String str=null;
        boolean bot=true;
         
        System.out.println("开始更新学生信息.....");
        do
        {
            System.out.print("请输入要更新的学生的学号:");
            Scanner sc=new Scanner(System.in);
            int s=sc.nextInt();
            boolean be=false;
            boolean br=false;  
             
            for(Student stud:StudentTest.set)
            {
                if(stud.num==s)
                {
                    be=true;    //修改标志位
                    System.out.println("请输入要更新的哪一项: 其中1代表学号,2代表姓名,3代表年龄,4代表语文成绩,5代表数学成绩,6代表英语成绩");
                    System.out.print("请输入:");
                    int n=sc.nextInt();
                     
                    switch(n)
                    {
                    case 1:
                    {
                        System.out.print("请输入修改后的学号:");
                        stud.num=sc.nextInt();
                        System.out.println("修改成功");
                        break;
                    }
                    case 2:
                    {
                        System.out.print("请输入修改后的姓名:");
                        stud.name=sc.next();
                        System.out.println("修改成功");
                        break;
                    }
                    case 3:
                    {
                        System.out.print("请输入修改后的年龄:");
                        stud.age=sc.nextInt();
                        System.out.println("修改成功");
                        break;
                    }
                    case 4:
                    {
                        System.out.print("请输入修改后的语文成绩:");
                        stud.s[0]=sc.nextDouble();
                        System.out.println("修改成功");
                         
                        stud.sum=stud.calculate_sum();  //重新更新总分
                        br=true;
                         
                        break;
                    }
                    case 5:
                    {
                        System.out.print("请输入修改后的数学成绩:");
                        stud.s[1]=sc.nextDouble();
                        System.out.println("修改成功");
                         
                        stud.sum=stud.calculate_sum();  //重新更新总分
                        br=true;
                         
                        break;
                    }
                    case 6:
                    {
                        System.out.print("请输入修改后的英语成绩:");
                        stud.s[2]=sc.nextDouble();
                        System.out.println("修改成功");
                         
                        stud.sum=stud.calculate_sum();  //重新更新总分
                        br=true;
                         
                        break;
                    }
                     
                    }
                             
                }
            }
             
            if(br==true)
            {
                Object[]obj=StudentTest.set.toArray();
                Arrays.sort(obj);
                StudentTest.set.clear();
                 
                for(int i=0;i<obj.length;i++)
                {
                    Student st=(Student)obj[i];
                    StudentTest.set.add(st);
                }
            }
             
             
            if(be==false)
            {
                System.out.println("你要修改的学生的学号不存在");
            }
             
            System.out.println("如果继续修改,则输入 %,否则输入#");
            System.out.print("请输入:");
            str=sc.next();
             
            if(!(str.equals("%")||str.equals("#")))
            {
                bot=false;
                System.out.println("你输入有误,已强制结束更新操作");
            }
             
        }while(str.equals("%") && bot);
         
        return bot;
    }
     
}

 调用

//StudentTest
 
package com.sran.www;
 
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
 
public class StudentTest {
    static Set<Student>set=null;  //定义集合,存储学生信息
     
    public void fun()
    {
        int i; 
        boolean b,bt;
        set=new TreeSet<Student>(); 
        do{
            int flag=0;
            System.out.println("请输入1,2,3,4,5   1代表录入,2代表删除,3代表查找,4代表显示学生总成绩排名,5代表更新某个学生数据");
            System.out.print("请输入:");
            Scanner sc=new Scanner(System.in);
            int n=sc.nextInt();
             
            switch(n)
            {
            case 1:
            {
                b=Student.insert();
                if(b==true)
                {
                    System.out.println("正常结束录入操作.....");
                }
                flag=1;
                break;
            }
            case 2:
            {
                b=Student.delete();
                if(b==true)
                {
                    System.out.println("正常结束删除操作.....");
                }
                flag=1;
                break;
            }
            case 3:
            {
                b=Student.seek();
                if(b==true)
                {
                    System.out.println("正常结束查询操作.....");
                }
                flag=1;
                break;
            }
            case 4:
            {
                System.out.println("开始排名学生信息.....");
                System.out.println("学生按总成绩排名:");
                for(Student stud:set)
                {
                    System.out.println(stud);
                    System.out.println();
                }
                 
                System.out.println("正常结束排名操作.....");
                flag=1;
                break;
            }
             
            case 5:
            {
                b=Student.update();
                if(b==true)
                {
                    System.out.println("正常结束更新操作.....");
                }
                flag=1;
                break;
            }
             
            }
            if(flag==0)
            {
                System.out.println("你输入有误,请重新输入");
            }
             
            System.out.println();
             
            do
            {  
                bt=false;
                System.out.println("继续所有学生信息操作,请输入1    退出学生信息系统 ,请输入0");
                System.out.print("请输入:");
                i=sc.nextInt();
                 
                if(i!=1 && i!=0)
                {
                    bt=true;
                    System.out.println("输入有误,请重新输入");
                }
                 
            }while(bt==true);
                 
        }while(i==1);
         
        System.out.println("你已退出学生信息系统.....");
         
    }
     
    public static void main(String[] args) {
        new StudentTest().fun();
    }
}

 

//student.java
 
package com.sran.www;
 
import java.util.Arrays;
import java.util.Scanner;
 
public class Student implements Comparable<Student>{
    int num;
    String name;
    int age;
    double[]s;
    double sum; //存储学生的总分
     
    public Student()
    {
        s=new double[3];
    }
 
    public static boolean insert()
    {
        Scanner sc=new Scanner(System.in);
        String str=null;
        boolean be=true;
         
        do
        {
            Student stud=new Student();
            System.out.println("开始录入学生的相关信息.....");
            System.out.print("学号:");
            stud.num=sc.nextInt();
            System.out.print("姓名:");
            stud.name=sc.next();
            System.out.print("年龄:");
            stud.age=sc.nextInt();
             
            System.out.println("依次输入三门课的成绩:");
            System.out.print("语文:");
            stud.s[0]=sc.nextDouble();
            System.out.print("数学:");
            stud.s[1]=sc.nextDouble();
            System.out.print("英语:");
            stud.s[2]=sc.nextDouble();
             
            stud.sum=stud.calculate_sum();
                 
            StudentTest.set.add(stud);
             
            System.out.println("如果继续录入,则输入%,否则输入 #");
            System.out.print("请输入:");
            str=sc.next();
             
            if(!(str.equals("%")||str.equals("#")))
            {
                be=false;
                System.out.println("你输入有误,已强制结束录入操作");
            }
             
        }while(str.equals("%") && be);
         
        return be;
    }
     
    public static boolean delete()
    {
        String str=null;
        boolean be=true;
        System.out.println("开始删除学生的相关信息.....");
        do
        {
            System.out.print("请输入要删除的学生的学号:");
            Scanner sc=new Scanner(System.in);
            int s=sc.nextInt();
             
            boolean flag=false//设一标志位
             
            for(Student stud:StudentTest.set)
            {
                if(s==stud.num)
                {
                    StudentTest.set.remove(stud);
                    flag=true;
                    System.out.println("已删除该学生的信息");
                    break;
                }
             
            }
             
            if(flag==false)
            {
                System.out.println("你要删除的学生的学号不存在");
            }
             
            System.out.println("如果继续删除,则输入 %,否则输入#");
            System.out.print("请输入:");
            str=sc.next();
             
            if(!(str.equals("%")||str.equals("#")))
            {
                be=false;
                System.out.println("你输入有误,已强制结束删除操作");
            }
             
        }while(str.equals("%") && be);
         
        return be;
    }
     
     
    public static boolean seek()
    {
        String str=null;
        boolean be=true;
        System.out.println("开始查找学生的相关信息.....");
        do
        {
            System.out.print("请输入要查找的学生的学号:");
            Scanner sc=new Scanner(System.in);
            int s=sc.nextInt();
             
            boolean flag=false;
             
            for(Student stud:StudentTest.set)
            {
                if(s==stud.num)
                {
                    flag=true//修改标志位
                    System.out.println("你要查找的学生的信息:");
                     
                    System.out.println("学号:"+stud.num);
                    System.out.println("姓名:"+stud.name);
                    System.out.println("年龄:"+stud.age);
                    System.out.println("语文成绩:"+stud.s[0]);
                    System.out.println("数学成绩:"+stud.s[1]);
                    System.out.println("英语成绩:"+stud.s[2]);
                     
                    break;
                }
            }
             
            if(flag==false)
            {
                System.out.println("你要查找的学生的学号不存在");
            }
             
             
            System.out.println("如果继续查找,则输入 %,否则输入#");
            System.out.print("请输入:");
            str=sc.next();
             
            if(!(str.equals("%")||str.equals("#")))
            {
                be=false;
                System.out.println("你输入的有误,已强制结束查询操作");
            }
             
        }while(str.equals("%") && be);
             
        return be;
    }
     
    public double calculate_sum()
    {  
        sum=s[0]+s[1]+s[2];
        return sum;
    }
 
    @Override
    public int compareTo(Student stud) {
        this.sum=this.calculate_sum();
        stud.sum=stud.calculate_sum();
         
        if(this.sum>stud.sum) return -1;
        else if(this.sum<stud.sum) return 1;
        else
        {
            if(this.name.compareTo(stud.name)>0return -1;
            else if(this.name.compareTo(stud.name)<0return 1;
            else
                return 0;
        }
    }
     
    public String toString()
    {
        System.out.println("学号"+"\t\t"+"姓名"+"\t\t"+"年龄"+"\t\t"+"语文"+"\t\t"+"数学"+"\t\t"+"英语"+"\t\t"+"总分");
        return(num+"\t\t"+name+"\t\t"+age+"\t\t"+s[0]+"\t\t"+s[1]+"\t\t"+s[2]+"\t\t"+sum);
     
    }
     
    public static boolean update()
    {
        String str=null;
        boolean bot=true;
         
        System.out.println("开始更新学生信息.....");
        do
        {
            System.out.print("请输入要更新的学生的学号:");
            Scanner sc=new Scanner(System.in);
            int s=sc.nextInt();
            boolean be=false;
            boolean br=false;  
             
            for(Student stud:StudentTest.set)
            {
                if(stud.num==s)
                {
                    be=true;    //修改标志位
                    System.out.println("请输入要更新的哪一项: 其中1代表学号,2代表姓名,3代表年龄,4代表语文成绩,5代表数学成绩,6代表英语成绩");
                    System.out.print("请输入:");
                    int n=sc.nextInt();
                     
                    switch(n)
                    {
                    case 1:
                    {
                        System.out.print("请输入修改后的学号:");
                        stud.num=sc.nextInt();
                        System.out.println("修改成功");
                        break;
                    }
                    case 2:
                    {
                        System.out.print("请输入修改后的姓名:");
                        stud.name=sc.next();
                        System.out.println("修改成功");
                        break;
                    }
                    case 3:
                    {
                        System.out.print("请输入修改后的年龄:");
                        stud.age=sc.nextInt();
                        System.out.println("修改成功");
                        break;
                    }
                    case 4:
                    {
                        System.out.print("请输入修改后的语文成绩:");
                        stud.s[0]=sc.nextDouble();
                        System.out.println("修改成功");
                         
                        stud.sum=stud.calculate_sum();  //重新更新总分
                        br=true;
                         
                        break;
                    }
                    case 5:
                    {
                        System.out.print("请输入修改后的数学成绩:");
                        stud.s[1]=sc.nextDouble();
                        System.out.println("修改成功");
                         
                        stud.sum=stud.calculate_sum();  //重新更新总分
                        br=true;
                         
                        break;
                    }
                    case 6:
                    {
                        System.out.print("请输入修改后的英语成绩:");
                        stud.s[2]=sc.nextDouble();
                        System.out.println("修改成功");
                         
                        stud.sum=stud.calculate_sum();  //重新更新总分
                        br=true;
                         
                        break;
                    }
                     
                    }
                             
                }
            }
             
            if(br==true)
            {
                Object[]obj=StudentTest.set.toArray();
                Arrays.sort(obj);
                StudentTest.set.clear();
                 
                for(int i=0;i<obj.length;i++)
                {
                    Student st=(Student)obj[i];
                    StudentTest.set.add(st);
                }
            }
             
             
            if(be==false)
            {
                System.out.println("你要修改的学生的学号不存在");
            }
             
            System.out.println("如果继续修改,则输入 %,否则输入#");
            System.out.print("请输入:");
            str=sc.next();
             
            if(!(str.equals("%")||str.equals("#")))
            {
                bot=false;
                System.out.println("你输入有误,已强制结束更新操作");
            }
             
        }while(str.equals("%") && bot);
         
        return bot;
    }
     
}

以上是关于java 学生管理系统的主要内容,如果未能解决你的问题,请参考以下文章

学生管理系统Java简易版

基于java设计一个学生成绩管理系统,要求有界面,且有增加,查询,修改,删除,退出功能,代码如何写?

基于java设计一个学生成绩管理系统,要求有界面,且有增加,查询,修改,删除,退出功能,代码如何写?

如何用Java语言编写学生成绩管理系统

学生信息管理系统最简单源代码。

Java实训:学生信息管理系统