class student { String name; String sex; int age; int grade; public student(String name,String sex,int age,int grade){ this.name=name; this.sex=sex; this.age=age; this.grade=grade; } }
import java.util.Scanner; public class Main { static student[]stu; public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); while(sc.hasNext()){ int n=sc.nextInt(); stu=new student[n+1]; String name,sex; int age,grade; for(int i=0;i<n;i++){ name=sc.next(); sex=sc.next(); age=sc.nextInt(); grade=sc.nextInt(); stu[i]=new student(name,sex,age,grade); } student temp; for(int i=1;i<n;i++){ for(int j=0;j<n-i;j++){ if(stu[j].grade>stu[j+1].grade){ temp=stu[j];stu[j]=stu[j+1];stu[j+1]=temp; } } } for(int i=0;i<n;i++){ System.out.println(stu[i].name+" "+stu[i].sex+" "+stu[i].age+" "+stu[i].grade); } } sc.close(); } }
原文:http://www.cnblogs.com/watchfree/p/5336447.html