導航:首頁 > 編程語言 > java輸入n個學生的成績

java輸入n個學生的成績

發布時間:2023-08-21 02:13:08

java 輸入n個學生信息,然後通過程序得到所有學生信息按照成績從高到低排列。

publicstaticvoidmain(String[]args){
Comparator_Testct=newComparator_Test();//實例化
Set<Student>set=newTreeSet<Student>(ct);//定義一個Set集合,元素為Student類型(參數為ct,即Comparator_Test類型,就是說這個集合的比較規則是依據這個類型)
inti=1;//變數,保存人數
Iterator<Student>it=addset(set).iterator();//定義一個迭代器,其值為addset方法的返回值
System.out.println("姓名 "+"語文 "+"數學 "+"總分 "+"名次 ");//列印列表標頭信息
while(it.hasNext()){//如果迭代器中有元素,執行循環
Studentstu=it.next();//返回一個Student對象
System.out.println(stu.getName()+" "+stu.getChscore()+" "+stu.getMhscore()+" "+stu.getTotal()+" "+"第"+i+"名");//列印這個Student對象的信息
i++;//將人數進行累加
}
}

//根據系統輸入數據,返回一個Student類型的對象
publicstaticStudentadd(){

intchscore=0,mhscore=0;//整型變數分別保存語文與數學的成績
Scannerscan=newScanner(System.in);//系統輸入
System.out.println("輸入一個學生名稱");//提示信息
Stringname=scan.next();//保存學生名稱

Scannerscan1=newScanner(System.in);
System.out.println("輸入"+name+"的語文成績,必須是一個整數且不能大於100");
chscore=scan1.nextInt();

Scannerscan2=newScanner(System.in);
System.out.println("輸入"+name+"的數學成績,必須是一個整數且不能大於100");
mhscore=scan2.nextInt();

//初始化一個Student實例,並為其各個屬性賦值(通過系統輸入的數據)
Studentstu=newStudent();
stu.setName(name);
stu.setChscore(chscore);
stu.setMhscore(mhscore);
stu.setTotal(stu.getChscore()+stu.getMhscore());
returnstu;
}

//針對不同的系統操作,返回不同的集合對象
publicstaticSet<Student>addset(Set<Student>set){
set.add(add());//為集合添加一個Student對象
Scannerscanner=newScanner(System.in);//系統輸入
System.out.println("是否繼續添加學生?輸入"+"【是】"+"繼續添加,輸入"+"【否】"+"結束添加。");
Strings=scanner.next();
if(s.equals("是")){//繼續添加
addset(set);//遞歸調用
}else{
returnset;
}
returnset;//返回集合
}


就這樣吧····

閱讀全文

與java輸入n個學生的成績相關的資料

熱點內容
osx賬戶恢復配置文件 瀏覽:966
安卓m1卡破解 瀏覽:557
系統保護文件是什麼 瀏覽:200
冒險島老物品代碼 瀏覽:834
南航app如何綁定護照 瀏覽:140
公眾號小程序怎麼製作 瀏覽:24
同城買菜什麼網站 瀏覽:211
鴻蒙系統如何顯示大文件 瀏覽:484
文件解壓後可以刪掉哪些 瀏覽:357
怎麼清除c盤垃圾文件 瀏覽:720
js都要掌握哪些內容 瀏覽:906
四角號碼字典有哪幾個版本 瀏覽:869
資料庫入侵dede 瀏覽:700
日本用什麼app聽歌 瀏覽:562
cmd新建空文件 瀏覽:481
廣聯達app在哪裡找 瀏覽:125
javaudp網路編程 瀏覽:263
如何快速統計文件大小 瀏覽:395
dnf90版本奶爸寂靜9加點 瀏覽:506
5s升級ios811好嗎 瀏覽:773

友情鏈接