導航:首頁 > 編程語言 > 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個學生的成績相關的資料

熱點內容
微信文件怎麼填 瀏覽:87
燕十八老師精通mysql視頻教程 瀏覽:255
汽車保養數據怎麼清 瀏覽:629
pdf文件圖像打不開 瀏覽:176
msp430時鍾程序 瀏覽:660
查看sd卡文件系統格式 瀏覽:696
c盤中顯示隱藏文件 瀏覽:951
蘋果升級系統白屏 瀏覽:136
三菱gxplc編程軟體如何使用 瀏覽:710
海康威視手機app怎麼看不了 瀏覽:482
wordpress下載中心插件 瀏覽:402
微信限制字數是多少 瀏覽:20
策劃輸出主要從哪些文件來 瀏覽:174
網路營銷找什麼工作 瀏覽:372
tcl匹配文件名的正則表達式 瀏覽:461
音頻文件數據量為何8 瀏覽:534
有哪些分享學習的網站 瀏覽:174
小程序文件發到微信電腦如何打開 瀏覽:265
四星五星做號工具 瀏覽:920
qq郵箱怎麼發送視頻文件 瀏覽:453

友情鏈接