解题思路: 构造对象,重写比较方法
注意事项:
参考代码:
import java.util.Arrays; import java.util.Scanner; public class Main implements Comparable<Object> { String name,sex; int age,score; Main(String name,String sex,int age,int score){ this.name=name;this.sex=sex;this.age=age;this.score=score; } @Override public int compareTo(Object o) { int flag=1; //因为是升序,所以先默认就是按升序 Main o1=(Main)o; if(this.score<o1.score) //找出值小的 flag=-1; return flag; } public String toString() { return name+" "+sex+" "+age+" "+score; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n =in.nextInt(); Main arr[]= new Main[n]; for(int i=0;i<n;i++) { arr[i]= new Main(in.next(), in.next(), in.nextInt(), in.nextInt()); } Arrays.sort(arr); for(Main a:arr) System.out.println(a); in.close(); } }
0.0分
0 人评分