黑马程序员技术交流社区

标题: 总结今天的一道题 [打印本页]

作者: boom_pm2.5    时间: 2016-11-19 23:30
标题: 总结今天的一道题
上`干货
题文~
//键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),
//按照总分从高到低输出到控制台

--------------------------------------------------------------------------------------------------------
测试 代码区

import java.util.HashSet;
import java.util.Scanner;
import java.util.TreeSet;

//键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),
//按照总分从高到低输出到控制台
public class HomeWorke01 {
        public static void main(String[] args) {
                TreeSet<Student> ts = new TreeSet<>();
       
               
                while (ts.size()<5) {
                        //因为有 姓名, 所以我们把它 输入成 String类型的
                        System.out.println("请输入学生的信息(参照格式  姓名,语文成绩,数学成绩,英语成绩):");
                        Scanner sc = new Scanner(System.in);
                        String s1 = sc.nextLine();
                        Student s= new Student();
                        String str = ",";
                        String arr[] = s1.split(str);
                        s.setName(arr[0]);
                        s.setChinese(Integer.parseInt(arr[1]));
                        s.setMath(Integer.parseInt(arr[2]));
                        s.setEnglish(Integer.parseInt(arr[3]));
                        s.setSum(s.getChinese()+s.getEnglish()+s.getMath());
                        ts.add(s);
                }
                //因为有 姓名, 所以我们把它 输入成 String类型的
       
                for (Student student : ts) {
                        System.out.println(student);
                }
               
               
               
        }
}

----------------------------------------------------------------------------------------------------------------------------------
学生类  代码区
public class Student implements Comparable<Student>{
        private String name;
        private double chinese;
        private double math;
        private double english;
        private double sum;
        public Student() {
                super();
                // TODO Auto-generated constructor stub
        }
        public Student(String name, double chinese, double math, double english) {
                super();
                this.name = name;
                this.chinese = chinese;
                this.math = math;
                this.english = english;
        }
        public String getName() {
                return name;
        }
        public void setName(String name) {
                this.name = name;
        }
        public double getChinese() {
                return chinese;
        }
        public void setChinese(double chinese) {
                this.chinese = chinese;
        }
        public double getMath() {
                return math;
        }
        public void setMath(double math) {
               
                this.math = math;
        }
        public double getEnglish() {
                return english;
        }
        public void setEnglish(double english) {
                this.english = english;
        }
        public double getSum() {
                return sum;
        }
        public void setSum(double sum) {
                this.sum = sum;
        }
        @Override
        public String toString() {
                return "姓名" + name + ", 语文成绩:" + chinese + ",数学成绩:"
                                + math + ",英语成绩:" + english + "]";
        }
        @Override
        public int compareTo(Student o) {
                double num = o.sum - this.sum;
                return (int) (num==0? 1: num);
        }
       
}


----]-------------------------------------------------------------------------------------------------------------------

总结下:
这道题,用到了今天学习到的 treeset方法,
然后在自定义类中,重写 equals, hashcode, comparable 方法,

通过判断 sum值得大小, 返回 num值,然后就能 将成绩从大到小排列了 ..


作者: boom_pm2.5    时间: 2016-11-19 23:32
只要难在  从写方法上,到后面还要家条件什么的.




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2