从键盘输入10个学生的数学、
英语和计算机的成绩,
然后按照平均成绩由高到低的顺序
依次输出,平均成绩相同的,
依次按照数学、英语、计算机
的成绩由低到高的顺序输出。

解决方案 »

  1.   

    把学生写成个类,然后放到list里
      

  2.   

    import java.io.*;
    import java.util.*;class  Test
    {
    public static void main(String[] args) throws Exception
    {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("请输入数学、英语、计算机成绩,以逗号分隔:");

    String buffer = "";
    String []temp = null;
    float math = 0.0f;
    float english = 0.0f;
    float computer = 0.0f;
    Set<Score> scoreSet = new TreeSet<Score>(); for(int i = 0; i < 10; i++){ buffer = reader.readLine();
    temp = buffer.split(",");

    math = Float.parseFloat(temp[0]);
    english = Float.parseFloat(temp[1]);
    computer = Float.parseFloat(temp[2]); Score score = new Score(math,english,computer);
    score.setAvg((float)((math+english+computer)/3.0f)); scoreSet.add(score);
    }

    Score score = null;
    Iterator<Score>it = scoreSet.iterator();
    System.out.println("平均\t\t数学\t\t英语\t\t计算机\t\t");
    while(it.hasNext()){

    score = (Score)it.next();
    System.out.println( score.avg + "\t\t" + score.math + "\t\t" + score.english + "\t\t" + score.computer );
    }
    }
    }class Score implements Comparable
    {
    public float math;
    public float english;
    public float computer;
    public float avg; public void setAvg(float avg){ this.avg = avg;
    }

    public Score(float math,float english,float computer){ this.math = math;
    this.english = english;
    this.computer = computer; } public int compareTo(Object obj){

    if(obj == null){
    return -1;
    }
    if(! (obj instanceof Score)){
    return -1;
    }
    if(obj == this){
    return 0;
    } Score o = (Score)obj; //平均分不等
    if( o.avg != this.avg){

    return (o.avg<this.avg?1:-1);
    }
    //平均分相等
    else{
    //数学相等
    if(this.math == o.math){

    //英语相等
    if(this.english == o.english){ if(this.computer == o.computer){
    return 0;
    }else{
    return (this.computer > o.computer?1:-1);
    }
    }
    //英语不等
    else {
    return (this.english > o.english?1:-1);
    } }
    //数学不等
    else{

    return -1;
    }
    }
    }
    }
      

  3.   

    汗,楼上太强,呵呵。帮个忙,我有一个问题,到现在还没有搞定,帮我看下:http://topic.csdn.net/u/20100611/20/8ceab9f5-68b1-4f6c-aec7-05280ae5f190.html
      

  4.   

    看到楼主题目不难,我就忍不住的做了出来.
    是完整的.public class Test
    {
    public static void main(String[] args) 
    throws NumberFormatException, IOException {
    for(int i=0;i<10;i++)
    {
    new Student();
    }
    }
    }class Student implements Comparable<Student>
    {
    int x,y,z;//成绩
    int t;  //平均分
    int i ;//学生编号
    static int r = 1 ;//学生数量
    static TreeSet<Student> s =new TreeSet<Student>();//所有学生的集合
    BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
    String str = null;
    Student() throws NumberFormatException, IOException
    {
    i=r; //学生编号赋值
    System.out.println("请输入第"+i+"个学生的数学成绩");
    while(!((str=bfr.readLine()).matches("\\d{1,3}")&&Integer.parseInt(str)<120))
    {
    System.out.println("输入成绩不合法,成绩必须是数字并且小于120,请重新输入.");
    }
    x = Integer.parseInt(str);

    System.out.println("请输入第"+i+"个学生的英语成绩");
    while(!((str=bfr.readLine()).matches("\\d{1,3}")&&Integer.parseInt(str)<120))
    {
    System.out.println("输入成绩不合法,成绩必须是数字并且小于120,请重新输入.");
    }
    y = Integer.parseInt(str);

    System.out.println("请输入第"+i+"个学生的计算机成绩");
    while(!((str=bfr.readLine()).matches("\\d{1,3}")&&Integer.parseInt(str)<120))
    {
    System.out.println("输入成绩不合法,成绩必须是数字并且小于120,请重新输入.");
    }
    z = Integer.parseInt(str);

    t = (x+y+z)/3;// 平均成绩
    s.add(this); //学生添加到集合
    r++;  //学生数量增加
    if(r==11) //学生数量到10个时调用打印方法
    print();
    }
    void print()
    {
    Iterator i = s.iterator();
    while(i.hasNext())
    {
    System.out.println(i.next());
    }
    }
    public int compareTo(Student o) {
    if(this.t>o.t)return -1;
    if(this.t<o.t)return 1;
    else
    {
    if(this.x>o.x)return -1;
    if(this.x<o.x)return 1;
    if(this.y>o.y)return -1;
    if(this.y<o.y)return 1;
    if(this.z>o.z)return -1;
    if(this.z<o.z)return 1;
    }
    return 0;
    }
       
       public String toString()
       {
       return "第"+i+"个学生的平均成绩: "+t+"\t数学成绩: "+x+"\t英语成绩: "+y+
       "\t计算机成绩: "+z;
       }
    }
      

  5.   

    import java.io.*;
    import java.util.*;public class Demo1 { public static void main(String[] args) throws Exception {
    int n = 10;
    Score[] score = new Score[n];
    BufferedReader buf = new BufferedReader(
    new InputStreamReader(System.in));
    for (int i = 0; i < n; i++) {
    System.out.println("请输入第" + (i + 1) + "个 学生的成绩:数学、英语、计算机");
    float math = Float.parseFloat(buf.readLine());
    float english = Float.parseFloat(buf.readLine());
    float computer = Float.parseFloat(buf.readLine());
    Score sc = new Score(math, english, computer);
    score[i] = sc;
    }
    Arrays.sort(score);
    for (int i = 0; i < score.length; i++) {
    System.out.println(score[i]);
    }
    }
    }class Score implements Comparable<Score> {
    private float english;
    private float computer;
    private float math;
    private float avg = avgScore(english, computer, math); public Score(float math, float english, float computer) {
    this.english = english;
    this.computer = computer;
    this.math = math;
    } public float avgScore(float math, float english, float computer) {
    return (english + computer + math) / 3;
    } public String toString() {
    return "英语成绩:" + this.english + ",计算机成绩:" + this.computer + ",数学成绩:"
    + this.math + "平均分是:" + avgScore(english, computer, math);
    } public int compareTo(Score o) {
    int res = this.avg > o.avg ? 1 : (this.avg == o.avg ? 0 : -1);
    if (res == 0) {
    res = this.math > o.math ? 1 : (this.math == o.math ? 0 : -1);
    }
    if (res == 0) {
    res = this.english > o.english ? 1 : (this.english == o.english ? 0
    : -1);
    }
    if (res == 0) {
    res = this.computer > o.computer ? 1
    : (this.computer == o.computer ? 0 : -1);
    }
    return res;
    }
    }
      

  6.   

    小小的YD一下,看错标题了,看成 用JAVA做,射了!
    唉,都是让猫扑给害的~
      

  7.   

    为什么我上学的时候没有csdn,郁闷