【java】王道計算機考研機試指南例題java版
阿新 • • 發佈:2019-02-20
package com.qq.demo1; import java.util.*; class Student implements Comparable { String name; int age; int score; public Student(String name, int age, int score) { this.name = name; this.age = age; this.score = score; } @Override // 過載比較規則 public int compareTo(Object o) { Student s = (Student) o; if (this.score != s.score) { return (this.score - s.score); } else { char a1 = this.name.charAt(0); char a2 = s.name.charAt(0); return (a1 - a2); } } public String toString() { return this.name + " " + this.age + " " + this.score; } } public class Main { public static void main(String[] args) { Scanner c = new Scanner(System.in); int n = c.nextInt(); ArrayList t = new ArrayList(); for (int i = 1; i <= n; i++) { String a = c.next(); int b1 = c.nextInt(); int b2 = c.nextInt(); Student s1 = new Student(a, b1, b2); t.add(s1); } Collections.sort(t); for (Object a : t) { System.out.print(a); } } }