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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© baoyumin 中级黑马   /  2016-2-23 23:10  /  333 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. public class 学生成绩 {

  2.         /**
  3.          .学生类包含四个属性:名字,语数外英的成绩。要求键盘录入五个学生的信息,
  4.          按照他们的总分从大到小排序,之后写入到d:\number.txt文件中。
  5.          * @throws IOException
  6.          */
  7.         public static void main(String[] args) throws IOException {
  8.     Scanner sc=new Scanner(System.in);
  9.     TreeSet<student>t=new TreeSet<student>(new Comparator<student>() {

  10.                 @Override
  11.                 public int compare(student s1, student s2) {
  12.                         int x=s2.getSum()-s1.getSum();
  13.                         return x==0?1:x;
  14.                 }
  15.         });
  16.     while(t.size()<3){
  17.             System.out.println("请输入学生成绩格式如下:姓名,语文,数学,英语");
  18.             String s=sc.nextLine();
  19.             String s1=",";
  20.             String[]s2=s.split(s1);
  21.             int yvwen=Integer.parseInt(s2[1]);
  22.             int math=Integer.parseInt(s2[2]);
  23.             int english=Integer.parseInt(s2[3]);
  24.             t.add(new student(s2[0],yvwen,math,english));
  25.     }
  26.     BufferedWriter bw=new BufferedWriter(new FileWriter("d:\\number.txt"));
  27.     for (student st : t) {
  28.                 bw.append(st.toString());
  29.                 bw.newLine();
  30.         }
  31.     bw.close();
  32.         }

  33. }
  34. class student{
  35.         private String name;
  36.         private int yvwen;
  37.         private int math;
  38.         private int english;
  39.         private int sum;
  40.         public student() {
  41.                 super();
  42.                
  43.         }
  44.         public student(String name, int yvwen, int math, int english) {
  45.                 super();
  46.                 this.name = name;
  47.                 this.yvwen = yvwen;
  48.                 this.math = math;
  49.                 this.english = english;
  50.                 this.sum=yvwen+math+english;
  51.         }
  52.         @Override
  53.         public String toString() {
  54.                 return "student [name=" + name + ", yvwen=" + yvwen + ", math=" + math
  55.                                 + ", english=" + english + ", sum=" + sum + "]";
  56.         }
  57.         public int getSum(){
  58.                 return sum;
  59.         }
  60. }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马