JAVA成绩排序名字代码 java成绩排序名字代码怎么输入( 二 )


import java.util.Collections;
public class Test {
public static void main(String[] args) {
//注意JAVA成绩排序名字代码 , 只能用对象类型,不可以使用简单类型 如int[] num则报错
Integer[] num = {5,8,3,9,1};
//如果是num是List或 Set,则用Collections.sort(num,Collections.reverseOrder());
Arrays.sort(num,Collections.reverseOrder());
for(int i=0;inum.length;i++){
System.out.println(num[i]);
}
}
}
设计一个给班级学生成绩排序的java程序,具体要求如下按照题目要求编写的Java程序如下(注意 以下程序全部放在Main.java文件中)
class student{
String name;
int score;
public student(String name,int score){
this.name=name;
this.score=score;
}
String studentInfo(){
return "name="+this.name+",score="+this.score;
}
}
public class Main{
public static void main(String[] args){
student sty[]=new student[5];
sty[0]=new student("zhangsan",67);
sty[1]=new student("lisi",75);
sty[2]=new student("wangwu",57);
sty[3]=new student("zhaoliu",88);
sty[4]=new student("ruanqi",93);
student stu[]=new student[5];
for(int i=0;isty.length;i++){
stu[i]=sty[i];
}
for(int i=0;istu.length-1;i++){
for(int j=0;jstu.length-i-1;j++){
if(stu[j].scorestu[j+1].score){
student temp=stu[j];
stu[j]=stu[j+1];
stu[j+1]=temp;
}
}
}
for(int i=0;istu.length;i++){
System.out.println(stu[i].studentInfo());
}
}
}
求java中类似学生信息管理系统中按学号,按姓名排序的代码import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Sort {
public static void main(String[] args) {
Student p1 = new Student(1001, "小明", 20);
Student p2 = new Student(1002, "小红", 21);
Student p3 = new Student(1003, "小黑", 19);
ListStudent list = new ArrayListStudent();
list.add(p1);
list.add(p2);
list.add(p3);
Collections.sort(list, new ComparatorStudent() {
/*
* int compare(Student o1, Student o2) 返回一个基本类型的整型,返回负数表示:o1 小于o2 , 
* 返回0 表示:o1和o2相等,返回正数表示:o1大于o2 。
*/
public int compare(Student o1, Student o2) {
// 按照学生的学号进行升序排列
if (o1.getId()o2.getId()) {
return 1;
}
if (o1.getId() == o2.getId()) {
return 0;
}
return -1;
}
});
write(list);
System.out.println("---------------------");
Collections.sort(list, new ComparatorStudent() {
/*
* int compare(Student o1, Student o2) 返回一个基本类型的整型, 返回负数表示:o1 小于o2,
* 返回0 表示:o1和o2相等,返回正数表示:o1大于o2 。
*/
public int compare(Student o1, Student o2) {
// 按照学生的年龄进行升序排列
if (o1.getAge()o2.getAge()) {
return 1;
}
if (o1.getAge() == o2.getAge()) {
return 0;
}
return -1;
}
});
write(list);
}
public static void write(ListStudent list) {
for (Student s : list) {
System.out.println(s.getId() + "\t" + s.getName() + "\t"
+ s.getAge());
}
}
}
public class Student {
private int id ;
private String name;
private int age;
//构造方法
public Student(int id,String name,int age){
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {

推荐阅读