概述
代码如下:
package patA;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
public class A1028 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
int now = sc.nextInt();
ArrayList<Student4> ar = new ArrayList<Student4>();
for (int i = 0; i < number; i++) {
Student4 stu = new Student4();
stu.id = sc.next();
stu.name = sc.next();
stu.score = sc.nextInt();
ar.add(stu);
}
// 用哪个比较器
if (now == 1) {
Comee1 com1 = new Comee1();
Collections.sort(ar, com1);
for (int i = 0; i < number; i++) {
System.out.println(ar.get(i).id + " " + ar.get(i).name + " " + ar.get(i).score);
}
}
if (now == 2) {
Comee2 com2 = new Comee2();
Collections.sort(ar, com2);
for (int i = 0; i < number; i++) {
System.out.println(ar.get(i).id + " " + ar.get(i).name + " " + ar.get(i).score);
}
}
if (now == 3) {
Comee3 com3 = new Comee3();
Collections.sort(ar, com3);
for (int i = 0; i < number; i++) {
System.out.println(ar.get(i).id + " " + ar.get(i).name + " " + ar.get(i).score);
}
}
}
}
class Student4 {
String id;
String name;
int score;
}
// 比较器1,比较id
class Comee1 implements Comparator<Student4> {
public int compare(Student4 stu1, Student4 stu2) {
if (Integer.parseInt(stu1.id) < Integer.parseInt(stu2.id)) {
return -1;
} else if (Integer.parseInt(stu1.id) > Integer.parseInt(stu2.id)) {
return 1;
} else {
return 0;
}
}
}
// 比较器2,比较名字
class Comee2 implements Comparator<Student4> {
public int compare(Student4 stu1, Student4 stu2) {
return stu1.name.compareTo(stu2.name);
}
}
// 比较器,比分数
class Comee3 implements Comparator<Student4> {
public int compare(Student4 stu1, Student4 stu2) {
if (stu1.score > stu2.score) {
return -1;
} else if (stu1.score < stu2.score) {
return 1;
} else {
return 0;
}
}
}
注意:
类名命名时要注意:Con, Prn, Aux, Clock$, Nul, Com1, Com2, Com3, Com4, Com5, Com6, Com7, Com8, Com9, Lpt(1~9) 均不可以.
最后
以上就是忧伤花瓣为你收集整理的PAT 甲级1028 (Java实现)的全部内容,希望文章能够帮你解决PAT 甲级1028 (Java实现)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复