日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 学生成绩排序

發布時間:2024/4/15 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 学生成绩排序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

編寫一個應用程序,用戶分別從兩個文本框輸入學術的姓名和分數,程序按成績排序將這些學生的姓名和分數顯示在一個文本區中。

程序運行效果如圖:

?

import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; public class Student extends JFrame { JLabel lName,lScore; JTextField tName,tScore; JTextArea taShow; JButton bSubmit; JPanel pan; Map<String,String> studentMap,resultMap; public static void main(String[] args) { new Student(); } public Student() { init(); click(); } public void init() { lName=new JLabel("姓名"); lScore=new JLabel("成績"); tName=new JTextField(10); tScore=new JTextField(10); bSubmit=new JButton("確定"); pan=new JPanel(); taShow=new JTextArea(); pan.add(lName); pan.add(tName); pan.add(lScore); pan.add(tScore); pan.add(bSubmit); add(pan,BorderLayout.NORTH); add(taShow, BorderLayout.CENTER); setTitle("學生成績排序"); setSize(400, 300); setVisible(true); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); validate(); studentMap=new HashMap<String,String>(); } private void click() { bSubmit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { save(); showMap(); } }); } private void save() { studentMap.put(tName.getText(),tScore.getText()); resultMap = sortMapByValue(studentMap);tName.setText(""); tScore.setText(""); } public static Map<String, String> sortMapByValue(Map<String, String> map) { if (map == null || map.isEmpty()) { return null; } Map<String, String> sortedMap = new LinkedHashMap<String, String>(); List<Map.Entry<String, String>> entryList = new ArrayList<Map.Entry<String, String>>(map.entrySet()); Collections.sort(entryList, new MapValueComparator()); Iterator<Map.Entry<String, String>> iter = entryList.iterator(); Map.Entry<String, String> tmpEntry = null; while (iter.hasNext()) { tmpEntry = iter.next(); sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue()); } return sortedMap; } private void showMap() { taShow.setText(""); for(Map.Entry<String,String> entry:resultMap.entrySet()) { taShow.append("姓名:"+entry.getKey()+" 成績:"+entry.getValue()+"\n"); } } } class MapValueComparator implements Comparator<Map.Entry<String, String>> { public int compare(Entry<String, String> s1, Entry<String, String> s2) { return s1.getValue().compareTo(s2.getValue()); } }

運行結果:

?

轉載于:https://www.cnblogs.com/songqinzhe/p/8037221.html

總結

以上是生活随笔為你收集整理的java 学生成绩排序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。