本帖最后由 走遍世界找寻你 于 2013-11-22 20:56 编辑
import itcast.cm.bean.Student2;
import java.util.Comparator;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
public class Test1 {
public static void main(String[] args) {
Set<Student2> set = new TreeSet<>(new Comparator<Student2>() {
public int compare(Student2 s1,Student2 s2){
int num = s1.getSum() - s2.getSum();
return num == 0 ? 1 : num ;
}
});
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生考试成绩:");
while(true) {
String line = sc.nextLine();
if("quit".equals(line))
break;
String[] arr = line.split(",");
set.add(new Student2(arr[0],Integer.parseInt(arr[1]),Integer.parseInt(arr[2]),
Integer.parseInt(arr[3])));
}
System.out.println("输出学生信息");
for (Student2 student2 : set) {
}
}
}
————————————————————————————————————————————
package itcast.cm.bean;
public class Student2 {
private String name;
private int math;
private int english;
private int chinese;
private int sum;
public Student2() {
super();
}
public Student2(String name, int math, int english, int chinese, int sum) {
super();
this.name = name;
this.math = math;
this.english = english;
this.chinese = chinese;
this.sum = this.chinese + this.english + this.math;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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 getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getSum() {
return sum;
}
@Override
public String toString() {
return "Student2 [name=" + name + ", math=" + math + ", english="
+ english + ", chinese=" + chinese + ", sum=" + sum + "]";
}
}
26行为什么会报错?什么原因啊?
|