Java作业-输入学生成绩并排序

来源:互联网 发布:淘宝扶持政策 编辑:程序博客网 时间:2024/04/29 19:27

目标效果:


代码:

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;/** * 编写一个应用程序,用户分别从两个文本框输入学生的姓名和分数,程序按成绩排序将这些学生的姓名和分数显示在一个文本区中。 * @author Vivinia * */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() {   @Overridepublic void actionPerformed(ActionEvent e) {save();showMap();}});}//点击按钮调用的保存方法private void save() {studentMap.put(tName.getText(),tScore.getText());resultMap = sortMapByValue(studentMap); //按Value进行排序 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());   //将元素存入List中,类型为entryCollections.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());   //将List中的元素遍历出来存入map}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());}}

按值排序有点麻烦,思想就是将map中元素遍历出来,使用sort即自定义比较器进行排序,然后重新存入map。


阅读全文
0 0
原创粉丝点击