解题思路:
方法一:创建学生类,然后冒泡排序
方法二:实现Arrays.Sort的Comparator排序方法
注意事项:
参考代码:
方法一
import java.util.Scanner; public class 成绩排序 { /** * @param args */ public static void main(String[] args) { Scanner sc=new Scanner(System.in); while (sc.hasNext()) { int n=sc.nextInt(); Student stu[]=new Student[n]; for (int i = 0; i < n; i++) { String name=sc.next(); int age=sc.nextInt(); int score=sc.nextInt(); stu[i]=new Student(name, age, score); } //冒泡排序 for (int i = 0; i <n; i++) { for (int j = 0; j <n-i-1; j++) { if (stu[j].Score>stu[j+1].Score) { Student temp=stu[j]; stu[j]=stu[j+1]; stu[j+1]=temp; }else if (stu[j].Score==stu[j+1].Score) { if (stu[j].Name.compareTo(stu[j+1].Name)>0) { Student temp=stu[j]; stu[j]=stu[j+1]; stu[j+1]=temp; }else if(stu[j].Name.compareTo(stu[j+1].Name)==0){ if (stu[j].Age>stu[j+1].Age) { Student 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].Age+" "+stu[i].Score); } } } //学生类 public static class Student{ String Name; int Age; int Score; public Student (String name,int age,int score) { this.Name=name; this.Age=age; this.Score=score; } public Student () { } } }
方法二
import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; public class 成绩排序2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); while (sc.hasNext()) { int n=sc.nextInt(); sc.nextLine(); Student stu[]=new Student[n]; for (int i = 0; i < n; i++) { String name=sc.next(); int age=sc.nextInt(); int score=sc.nextInt(); stu[i]=new Student(name, age, score); } //排序 Arrays.sort(stu,new Comparator<Student>() { public int compare(Student stu1,Student stu2){ if (stu1.Score>stu2.Score) { return 1; }else if(stu1.Score==stu2.Score){ if (stu1.Name.compareTo(stu2.Name)>0) { return 1; }else if (stu1.Name.compareTo(stu2.Name)==0) { if (stu1.Age>stu2.Age) { return 1; } } } return -1; } }); //输出 for(int i=0;i<n;i++){ System.out.println(stu[i].Name+" "+stu[i].Age+" "+stu[i].Score); } } } //学生类 public static class Student{ String Name; int Age; int Score; public Student (String name,int age,int score) { this.Name=name; this.Age=age; this.Score=score; } } }
0.0分
0 人评分
点我有惊喜!你懂得!浏览:2071 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:703 |
C语言程序设计教程(第三版)课后习题11.3 (C语言代码)浏览:1051 |
九宫重排 (C++代码)浏览:2160 |
C语言程序设计教程(第三版)课后习题4.9 (C语言代码)浏览:896 |
C语言程序设计教程(第三版)课后习题8.2 (C语言代码)浏览:5226 |
C语言训练-求s=a+aa+aaa+aaaa+aa...a的值 (C语言代码)浏览:612 |
WU-输出九九乘法表 (C++代码)浏览:1654 |
C语言程序设计教程(第三版)课后习题6.2 (C语言代码)浏览:703 |
简单的a+b (C语言代码)浏览:579 |