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

熱點內容
網路編程udp 瀏覽:148
加密壓縮文件如何打開 瀏覽:56
微軟編程軟體有哪些 瀏覽:736
linux目錄中創建文件夾許可權設置密碼 瀏覽:759
word文檔正式文件模版 瀏覽:247
linux文件系統的類型是 瀏覽:111
蘋果的無線傳輸文件找不到了 瀏覽:102
密件文件名能出現在通知嗎 瀏覽:832
編寫一個web應用程序 瀏覽:350
哪些場所網路好 瀏覽:171
華為手機怎麼找到以前安裝過的app 瀏覽:100
49塊一單約人陪的app是什麼 瀏覽:570
ug文件格式圖片 瀏覽:24
興趣班編程課是學什麼 瀏覽:879
怎麼才能加微信醫葯群 瀏覽:601
微信表情小蘿莉親吻 瀏覽:876
我國應如何做好互聯網網路空間的治理工作 瀏覽:777
今日最新聞投票app 瀏覽:53
aecs6卸載工具 瀏覽:645
視頻文件包含的信息 瀏覽:238

友情鏈接