import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Struct;
import java.util.*;public class Student {
    String id;
    String name;
    int score;
    String code;    public Student(String id, String name, int score, String code) {
        this.id = id;
        this.name = name;
        this.score = score;
        this.code = code;
    }    public String getId() {
        return id;
    }    public void setId(String id) {
        this.id = id;
    }    public String getName() {
        return name;
    }    public void setName(String name) {
        this.name = name;
    }    public double getScore() {
        return score;
    }    public void setScore(int score) {
        this.score = score;
    }    public String getCode() {
        return code;
    }    public void setCode(String code) {
        this.code = code;
    }    public String toString() {
        String s = "";
        s += id + ",";
        s += name + ',';
        s += code + ",";
        s += score;
        return s;
    }    public static void main(String args[]) {
        Comparator[] cmps = new Comparator[4];        cmps[0] = new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                return ((Student)o1).getId().compareTo(((Student)o2).getId());
            }
        };
        cmps[1] = new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                return ((Student)o1).getName().compareTo(((Student)o2).getName());
            }
        };
        cmps[2] = new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                return ((Student)o1).getCode().compareTo(((Student)o2).getCode());
            }
        };
        cmps[3] = new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if (((Student)o1).getScore() > ((Student)o2).getScore())
                    return 1;
                else if (((Student)o1).getScore() == ((Student)o2).getScore())
                    return 0;
                else
                    return -1;
            }
        };//        String fileName=args[0];
        List<Student> students = new ArrayList<>();
        String line="";
        Scanner sc = new Scanner(System.in);
//        try {
//            BufferedReader in = new BufferedReader(new FileReader(fileName));
//            line = in.readLine();
        line = sc.nextLine();
            int count = Integer.parseInt(line);
            for (int i = 0; i < count; i++) {
//                line = in.readLine();
            line=sc.nextLine();
                String[] l = line.split(",");
                String id = l[0];
                String name = l[1];
                String code = l[2];
                int score = Integer.parseInt(l[3]);
                Student stu = new Student(id, name, score, code);
                students.add(stu);
            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        while(true) {
            line = sc.nextLine();
            String[] l = line.split(" ");
            int field = Integer.parseInt(l[0]);
            if (field == 0)
                return;
            String flag = l[l.length - 1];
            students.sort(cmps[field-1]);
            if (flag.equals("A")) {
                for (int i = 0; i < students.size(); i++) {
                    System.out.println(students.get(i));
                }
            } else if (flag.equals("D")) {
                for (int i = students.size() - 1; i >= 0; i--) {
                    System.out.println(students.get(i));
                }
            }
            System.out.println("");
//            field = sc.nextInt();
//            if (field == 0)
//                break;
        }
    }
}

解决方案 »

  1.   

    这个用eclipses测试好点
      

  2.   

     students.sort(cmps[field-1]);  这里应该 Collections.sort(students,cmps[field-1]); 吧
      

  3.   

    你程序里compareTo 也有问题,compareTo只能用来比较数据类型,你ID 和name都是字符串
      

  4.   

    equals比较的地址。
    equals的用法:
    ①:
            int [] a = {1,2,3};
            int [] b = {1,2,3};
            System.out.println(a.equals(b)); 输出结果为false,词语句创建了两个对象,地址不同即使都是{1,2,3}也为false
    ②:此方法为正确比较两个值的正确方法。
            int [] a = {1,2,3};
            int [] b = {1,2,3};
            System.out.println(Arrays.equals(a,b));输出结果为true