A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 478883662 中级黑马   /  2015-11-8 18:27  /  854 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部的信息保存在文件中,可以实现对学生信息的学号查找、输出全部学生信息的功能。{:3_62:}

3 个回复

倒序浏览
这是我写的,你可以参考一下
  1. import java.util.*;
  2. import java.util.Scanner;
  3. public class Test {
  4.        
  5.         public static final Scanner s = new Scanner(System.in);
  6.         public static final ArrayList<Student> list = new ArrayList<Student>();
  7.         public static void main(String[] args) {
  8.                 home();
  9.         }
  10.        
  11.         public static void home(){        //主页
  12.                 while(true){
  13.                         System.out.println("1.录入学生信息     2.查询学生信息     3.删除学生信息     4.退出\n输入数字选择:");
  14.                         int num = s.nextInt();
  15.                         switch(num){
  16.                                 case 1:
  17.                                         inputInfo();
  18.                                         break;
  19.                                 case 2:
  20.                                         findInfo();
  21.                                         break;
  22.                                 case 3:
  23.                                         remove();
  24.                                         break;
  25.                                 case 4:
  26.                                         return;
  27.                         }
  28.                        
  29.                 }
  30.                
  31.         }
  32.         public static void findInfo(){        //查询信息
  33.                 System.out.println("1.升序     2.降序      3.返回\n输入数字选择查询方式(两科的总分):");
  34.                 int num = 0;
  35.                 while(true){
  36.                         num = s.nextInt();
  37.                         if(num==1){        //升序
  38.                                 Collections.sort(list,new Comparator<Student>(){
  39.                                         public int compare(Student s1, Student s2) {
  40.                                                 int n = new Integer(s1.getChinese()+s1.getMath()).compareTo(new Integer(s2.getChinese()+s2.getMath()));
  41.                                                 if(n==0)
  42.                                                         return s1.getName().compareTo(s2.getName());
  43.                                                 return n;
  44.                                         }
  45.                                 });
  46.                                 printAll();
  47.                         }
  48.                         else if(num==2){        //降序
  49.                                 Collections.sort(list,new Comparator<Student>(){
  50.                                         public int compare(Student s1, Student s2) {
  51.                                                 int n = new Integer(s2.getChinese()+s2.getMath()).compareTo(new Integer(s1.getChinese()+s1.getMath()));
  52.                                                 if(n==0)
  53.                                                         return s2.getName().compareTo(s1.getName());
  54.                                                 return n;
  55.                                         }
  56.                                 });
  57.                                 printAll();
  58.                         }
  59.                         else if(num==3)
  60.                                 break;
  61.                         else
  62.                                 System.out.println("输入错误,重新输入:");
  63.                 }
  64.                 return;
  65.         }
  66.         public static void remove(){        //删除信息
  67.                 System.out.print("输入需要删除的姓名:");
  68.                 String name = s.next();
  69.                 Iterator<Student> it = list.listIterator();
  70.                 boolean flag = false;
  71.                 while(it.hasNext()){
  72.                         Student str = it.next();
  73.                         if(name.equals(str.getName())){
  74.                                 it.remove();
  75.                                 flag = true;
  76.                         }
  77.                 }
  78.                 if(flag)
  79.                         System.out.println(name+" 删除成功!");
  80.                 else
  81.                         System.out.println("删除失败,未找到该名字!");
  82.                 System.out.println("1.继续     2.返回\n输入数字选择:");
  83.                 while(true){
  84.                         int num = s.nextInt();
  85.                         if(num==1){
  86.                                 remove();
  87.                                 return;
  88.                         }
  89.                         else if(num==2)
  90.                                 break;
  91.                         else
  92.                                 System.out.println("输入错误,重新输入:");
  93.                 }
  94.                 return;
  95.         }
  96.         public static void printAll(){        //打印信息
  97.                 int num = 1;
  98.                 System.out.println("--------------(查询)----------------");
  99.                 if(list.size()>0)
  100.                         for(Student stu:list){
  101.                                 System.out.println(num++ +".姓名:"+stu.getName()+"|语文成绩:"+stu.getChinese()+"|数学成绩:"+stu.getMath()+"---|总分:"+(stu.getChinese

  102. ()+stu.getMath()));
  103.                         }
  104.                 else
  105.                         System.out.println("没有学生信息!");
  106.                 System.out.println("-----------------------------------");
  107.         }
  108.         public static void inputInfo(){        //输入信息
  109.                 System.out.println("输入学生姓名:");
  110.                 String name = s.next();
  111.                 System.out.println("输入数学成绩:");
  112.                 int Math = s.nextInt();
  113.                 System.out.println("输入语文成绩:");
  114.                 int chinese = s.nextInt();
  115.                 list.add(new Student(name,Math,chinese));
  116.                 System.out.println("1.返回     2.继续录入\n输入数字选择:");
  117.                 while(true){
  118.                         int num = s.nextInt();
  119.                         if(num==1)
  120.                                 break;
  121.                         else if(num==2){
  122.                                 inputInfo();
  123.                                 return;
  124.                         }
  125.                         else
  126.                                 System.out.println("输入错误,重新输入:");
  127.                 }
  128.                 return;
  129.         }
  130. }
  131. class Student{        //学生类
  132.         private String name;
  133.         private int chinese;
  134.         private int math;
  135.         Student(String name,int math,int chinese){
  136.                 this.name = name;
  137.                 this.chinese = chinese;
  138.                 this.math = math;
  139.         }
  140.         public String  getName(){
  141.                 return name;
  142.         }
  143.         public int getChinese(){
  144.                 return chinese;
  145.         }
  146.         public int getMath(){
  147.                 return math;
  148.         }
  149. }
复制代码
回复 使用道具 举报
没细看,只想说好牛,,,自己抓紧时间去学习吧。。。
回复 使用道具 举报
呵呵,有点难度啊,看看楼上代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马