当前位置: 代码迷 >> Java相关 >> 用java编一个学生成绩排序成绩 有学生的姓名 学号 三门课的成绩 然后对总分 ...
  详细解决方案

用java编一个学生成绩排序成绩 有学生的姓名 学号 三门课的成绩 然后对总分 ...

热度:1179   发布时间:2013-04-27 02:58:07.0
用java编一个学生成绩排序成绩 有学生的姓名 学号 三门课的成绩 然后对总分排序
import java.util.*;

public class student
{
    class Node
    {
         public String name ;
         public String ID;
         public int java ;
         public int os;
         public int PE;
         public int total;
    }
   public  Node data;
    public student next;
    public student creat()
    {
        student head,p,r,s1;
        int x1,x2,x3,x4;
        String n,i;
        head=r=null;
        System.out.print("请输入学生的姓名  学号   java成绩    os成绩     Internet 成绩");
       System.out.print("请输入一个x整形数据以0结束:");
        Scanner c= new Scanner(System.in);
        n=c.nextLine();
        Scanner c0= new Scanner(System.in);
        i=c0.nextLine();
        Scanner c1= new Scanner(System.in);
        x1=c1.nextInt();
        Scanner c2= new Scanner(System.in);
        x2=c2.nextInt();
        Scanner c3= new Scanner(System.in);
        x3=c3.nextInt();
        
        while (x1!=0)
        {
            student s11=new student();
            s11.data.name=n;
            s11.data.ID=i;
            s11.data.java=x1;
            s11.data.os=x2;
            s11.data.PE=x3;
            s11.data.total=s11.data.java+s11.data.os+s11.data.PE;
            if(head==null)
                head=s11;
            else
                r.next=s11;
               
            r=s11;
            Scanner b= new Scanner(System.in);
            n=b.nextLine();
            Scanner b0= new Scanner(System.in);
            i=b0.nextLine();
            Scanner b1= new Scanner(System.in);
            x1=b1.nextInt();
            Scanner b2= new Scanner(System.in);
            x2=b2.nextInt();
            Scanner b3= new Scanner(System.in);
            x3=b3.nextInt();
        }
        if(r!=null)
            r.next=null;
        return head;
    }
    public void out(student head)
    {
        student p;
        p=head;
        System.out.print("List is:");
        while(p!=null)
        {
            System.out.println("\t"+p.data.name+"\t"+p.data.ID+"\t"+p.data.java+"\t"+p.data.os+"\t"+p.data.PE+"\t"+p.data.total);
            p=p.next;
        }
    }
    public student sortup(student head)
    {
        student p=head,pre=null,s=null,q=null;
           head.next=null;
           while(p!=null)
           {
               s=p;
               p=p.next;
               pre=head;
               q=head.next;
               while(q!=null && q.data.total<s.data.total)
               {
                   pre=q;
                   q=q.next;
               }
               s.next=q;
               pre.next=s;
           }
        return head;
    }
    public student sortdown(student head)
    {
        student p=head,pre=null,s=null,q=null;
           head.next=null;
           while(p!=null)
           {
               s=p;
               p=p.next;
               pre=head;
               q=head.next;
               while(q!=null && q.data.total<s.data.total)
               {
                   pre=q;
                   q=q.next;
               }
               s.next=q;
               pre.next=s;
           }
        return head;
    }
    public static void main(String[] args) {
        student x,y;
        student a=new student();
        x=a.creat();
        a.out(x);
        a.sortdown(x);
        a.out(x);
        a.sortup(x);
        a.out(x);
        
    }
}
我是一个java初学者  我们老师布置了一个实验  用java编一个学生成绩管理的一个小程序 (学生人数为10人)
要求:输入姓名  学号  三门课的成绩  计算总成绩  然后对这10个学生的总成绩排序  我编了一个但是行不通 我是按c语言  链表的思想来用java做的  不知道那出了问题  求各位大侠不胜赐教  帮我把它该对  谢谢 !小女子不胜感激  !!!  4.30之前就要交 希望大侠们.。。。
搜索更多相关的解决方案: java  学生  public  import  null  

----------------解决方案--------------------------------------------------------
程序代码:

/**
定义一个学生的实体类
*/
public class Student {
    private String name;
    private String ID;
    private int java = 0;
    private int os = 0;
    private int PE = 0;
    private int total = 0;

    public Student() {

    }

    public Student(String name,String ID,int java, int os, int PE) {
        this.name = name;
                this.ID = ID;
                this.java = java;
        this.os = os;
        this.PE = PE;
        this.total = java + os + PE;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getID() {
        return ID;
    }

    public void setID(String iD) {
        ID = iD;
    }

    public int getJava() {
        return java;
    }

    public void setJava(int java) {
        this.java = java;
    }

    public int getOs() {
        return os;
    }

    public void setOs(int os) {
        this.os = os;
    }

    public int getPE() {
        return PE;
    }

    public void setPE(int pE) {
        PE = pE;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }
    public void out(){
        System.out.println("\t"+this.getJava()+"\t"+this.getOs()+"\t"+this.getPE()+"\t"+this.getTotal());
    }
}

---  下面是 运行方法 ----
public static void main(String[] args) {

                /**ArrayList需要通过collections类的sort方法来进行排序
                  如果想自定义排序方式则需要有类来实现Comparator接口并重写compare方法

                  调用sort方法时将ArrayList对象与实现Commparator接口的类的对象作为参数*
*/

        Comparator<Student> comparator = new Comparator<Student>() {
            public int compare(Student s1, Student s2) {

                return s2.getTotal()-s1.getTotal() ;
//s2.getTotal()-s1.getTotal()倒序 //s1.getTotal()-s2.getTotal()顺序

            }
        };
        ArrayList<Student> list = new ArrayList<Student>();
        for (int i = 0; i <= 2; i++) {
            Scanner scan = new Scanner(System.in);
            System.out.println("请输入学生的姓名  学号   java成绩    os成绩     Internet 成绩");
            Student s = new Student(scan.next(),scan.next(),scan.nextInt(), scan.nextInt(),
                    scan.nextInt());
            list.add(s);
        }
        Collections.sort(list,comparator);
        for (Student stu : list) {
            stu.out();
        }

    }

----------------解决方案--------------------------------------------------------
谢谢
----------------解决方案--------------------------------------------------------
非常感谢你给我的解答  有些地方我看不怎么懂  再次向你请教
为什么每一个Student的类的每一个属性都有   return xx  ,this.xx=xx 呢?是什么含义
public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
----------------解决方案--------------------------------------------------------
这个是实体类属性的set和get方法!设置值用set获取值用get。
----------------解决方案--------------------------------------------------------
我定义的属性是private
只能提供一个外部的获取方法才能 获取和修改

----------------解决方案--------------------------------------------------------
楼上的小程序写的合理,就是此程序不需要它的set get方法,没有涉及到实体注入,同时建议初学者将实体类和主方法分开写,逐渐培养良好的封装思想
----------------解决方案--------------------------------------------------------

----------------解决方案--------------------------------------------------------
厉害,没想到用这个
Comparator<Student> comparator = new Comparator<Student>() {
            public int compare(Student s1, Student s2) {

                return s2.getTotal()-s1.getTotal() ;
//s2.getTotal()-s1.getTotal()倒序 //s1.getTotal()-s2.getTotal()顺序

            }
        };
----------------解决方案--------------------------------------------------------
  相关解决方案