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

熱點內容
網路中常用的傳輸介質 瀏覽:518
文件如何使用 瀏覽:322
同步推密碼找回 瀏覽:865
樂高怎麼才能用電腦編程序 瀏覽:65
本機qq文件為什麼找不到 瀏覽:264
安卓qq空間免升級 瀏覽:490
linux如何刪除模塊驅動程序 瀏覽:193
at89c51c程序 瀏覽:329
怎麼創建word大綱文件 瀏覽:622
裊裊朗誦文件生成器 瀏覽:626
1054件文件是多少gb 瀏覽:371
高州禁養區內能養豬多少頭的文件 瀏覽:927
win8ico文件 瀏覽:949
仁和數控怎麼編程 瀏覽:381
項目文件夾圖片 瀏覽:87
怎麼在東芝電視安裝app 瀏覽:954
plc顯示數字怎麼編程 瀏覽:439
如何辨別假網站 瀏覽:711
寬頻用別人的賬號密碼 瀏覽:556
新app如何佔有市場 瀏覽:42

友情鏈接