黑马程序员技术交流社区
标题:
关于TreeSet集合
[打印本页]
作者:
敲程序的猿
时间:
2016-2-16 22:47
标题:
关于TreeSet集合
求助:这是一个作业题但是没有思路,求指导
需求: 键盘录入五个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低输出在控制台
作者:
姚成晖
时间:
2016-2-17 00:29
本帖最后由 姚成晖 于 2016-2-17 00:36 编辑
public class Test {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>(){
public int compare(Student s1,Student s2){
int num = s1.getSum()-s2.getSum();
return num==0?1:num;
}
});
while(ts.size()<5){
System.out.println("请输入学生及成绩,格式:name,30,30,30");
String line = sc.nextLine();
String [] str=line.split(",");
int chinese = Integer.parseInt(str[1]);
int math = Integer.parseInt(str[2]);
int english = Integer.parseInt(str[3]);
ts.add(new Student(str[0],chinese,math,english));
}
for(Student s:ts){
System.out.println("姓名"+s.getName()+",语文:"+s.getChinese()+",数学"+s.getMath()+",英语"+s.getEnglish()+",总分"+s.getSum());
}
}
}
public class Student {
private String name;
private int chinese;
private int math;
private int english;
Student(){}
Student(String name,int chinese,int math,int english){
this.name=name;
this.chinese=chinese;
this.math=math;
this.english=english;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setChinese(int chinese){
this.chinese=chinese;
}
public int getChinese(){
return chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
public int getSum(){
return chinese+math+english;
}
}
复制代码
作者:
姚成晖
时间:
2016-2-17 00:37
没有导包 你导下包就可以啦
作者:
tujiu888
时间:
2016-2-17 21:57
如果问题作出来了 再给你加点难度, 年龄从小到大,姓名按照字典顺序排序 把这个代码再敲出来 你这个问题就彻底搞懂了
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2