4題

4、設計一個類Student,該類包括姓名、學號和成績。設計一個方法,按照成績從高到低的順序輸出姓名、學號和成績信息。[選做題]


publicclassStudent?{

????privateintid;//學號

????privateString?name;//姓名

????privateintscore;//成績

????publicStudent(intid,?String?name,?intscore)?{

????????super();

????????this.id?=?id;

????????this.name?=?name;

????????this.score?=?score;

????}


????publicintgetId()?{

????????returnid;

????}


????publicvoidsetId(intid)?{

????????this.id?=?id;

????}


????publicString?getName()?{

????????returnname;

????}


????publicvoidsetName(String?name)?{

????????this.name?=?name;

????}


????publicintgetScore()?{

????????returnscore;

????}


????publicvoidsetScore(intscore)?{

????????this.score?=?score;

????}


????@Override

????publicString?toString()?{

????????return(id?+?"???"+?name?+?"???"+?score);

????}


}




importjava.util.Arrays;


publicclassTestStudent?{

????staticStudent[]?stus;

????publicstaticvoidsort(Student[]?stus){

????????intlen?=?stus.length;//學生個數(shù)

????????int[]?a?=?newint[len];

????????//記錄分數(shù)

????????for(inti?=?0;?i?<?len;?i++)?{

????????????a[i]?=?stus[i].getScore();

????????}


????????Arrays.sort(a);//升序排序


????????for(inti?=?a.length?-?1;?i?>=?0;?i--)?{

????????????for(intj?=?0;?j?<?stus.length;?j++)?{

????????????????if(stus[j].getScore()?==?a[i]){

????????????????????System.out.println(stus[j]);

????????????????}

????????????}

????????}

????}

????publicstaticvoidmain(String[]?args)?{

????????stus?=?newStudent[3];

????????stus[0]?=?newStudent(1,"a",87);

????????stus[1]?=?newStudent(2,"b",78);

????????stus[2]?=?newStudent(3,"c",99);

????????System.out.println("按分數(shù)從高到低排序:");

????????sort(stus);

????}

}

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容