请大神帮忙看一下,为什么运行可以通过,但排序后的成绩却输不出来呢,能否帮忙改一下?

public class student

{

String name,id;

double score1,score2,score3;

    double sum;

int mingci;

student(){

};

student(String n,String i,double s1,double s2,double s3)

{

name=n;

id=i;

score1=s1;

score2=s2;

score3=s3;

sum=s1+s2+s3;

}

static void selectsort(student a[])

{

for(int i=0;i<a.length;i++)

{

student t=new student();

for(int j=i;j<a.length;j++)

{

if(a[i].sum<a[j].sum)

{

t=a[i];

a[i]=a[j];

a[j]=t;

}

}

}

}

static void out(student a[])

{

for(int i=0;i<a.length;i++)

System.out.println(" "+"姓名:"+a[i].name+" "+"学号:"+a[i].id+"成绩:"+a[i].score1+" "+a[i].score2+" "+a[i].score3);

}

  static void print(student a[])

{

for(int i=0;i<a.length;i++)

a[i].mingci=i+1;

for(int i=0;i<a.length;i++)

    { if(a[i].sum==a[i-1].sum)

{a[i].mingci=a[i-1].mingci;}

else 

{a[i].mingci=a[i-1].mingci+1;}

System.out.println(" "+"姓名:"+a[i].name+" "+"学号:"+a[i].id+"总分:"+" "+a[i].sum+"名次:"+a[i].mingci);

}

}

public static void main(String args[])

{

student s[]=new student[5];

s[0]=new student("张三","001",80,90,85);

s[1]=new student("李斯","002",80,80,85);

s[2]=new student("王五","003",70,90,85);

s[3]=new student("赵六","004",80,90,80);

s[4]=new student("李云","005",80,75,85);

   System.out.println("排序前");

   student.out(s);

   student.selectsort(s);

   System.out.println("排序后");

   student.print(s);

}

}


月饼啦啦啦
浏览 1099回答 2
2回答

望远

a[i].mingci=a[i-1].mingci;//这一条语句有数组下标越界异常,当i==0时i-1==-1

望远

for(int i=0,k=0;i<a.length;i++)  {     a[i].mingci=++k;      for(int j=i+1;j<a.length;j++)      {         if(a[i].sum==a[j].sum)          {              a[j].mingci=a[i].mingci;              i=j;          }       }  }试试这个
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java