zoukankan      html  css  js  c++  java
  • 编号0-999的学生,为每位学生随机生成考试成绩(0-100分),找出10个最好成绩,学生获得相同分数最多的5个分数,并列出获得这几个分数的学生编号清单。

    package com.cn;

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.Random;

    /**
    * Created by Sam on 2017/1/9.
    */
    public class ToolClassPractice {
    public static void main(String[] args) {
    int[] score = new int[101];
    // 出现次数统计的集合---key出现的分数,value为出现的次数
    Map<Integer, Integer> map_times = new HashMap<Integer, Integer>();
    // 学生对象的集合---key为编号,value为分数
    Map<Integer, Integer> map_Stu = new HashMap<Integer, Integer>();
    // 生成1000个学生
    for (int i = 0; i < 1000; i++) {
    Random random = new Random();
    // 在0-100分之间随机产生分数
    int j = random.nextInt(101);
    // 统计每个分数所出现的次数
    score[j]++;
    map_Stu.put(i, j);
    }

    for (int q = 0; q <= 100; q++) {
    map_times.put(q, score[q]);
    }

    ToolClassPractice t = new ToolClassPractice();
    // 学生的Map集合转化为List集合
    List<Map.Entry<Integer, Integer>> list_Stu = new ArrayList<Map.Entry<Integer, Integer>>();
    // 出现的次数Map集合转化为List集合
    List<Map.Entry<Integer, Integer>> list_Times = new ArrayList<Map.Entry<Integer, Integer>>();

    // 将分数进行排序
    list_Stu = t.sortMapByValue(map_Stu);
    System.out.println("10个最高的分数为:");
    for (int a = 0; a < 10; a++) {
    System.out.println(list_Stu.get(a).getValue());
    }

    // 将次数集合进行排序
    list_Times = t.sortMapByValue(map_times);

    // 取得前5个次数集合
    List<Map.Entry<Integer, Integer>> list_TimesTop5 = new ArrayList<Map.Entry<Integer, Integer>>();
    for (int p = 0; p < 5; p++) {
    list_TimesTop5.add(list_Times.get(p));
    }
    // 得到分数最多的前五个分数集合的学生对象
    System.out.println("分数最多的前五个的学生对象:");
    for (Map.Entry<Integer, Integer> entryTimes : list_TimesTop5) {
    int count = 0;
    for (Map.Entry<Integer, Integer> entry : list_Stu) {
    count++;
    if (count == 1) {
    System.out.println("---" + entryTimes.getKey() + "的分数有" + entryTimes.getValue() + "人" + "---");
    }

    // 次数集合的分数 与 学生集合的分数 相等
    if (entryTimes.getKey() == entry.getValue()) {
    System.out.println("学号:" + entry.getKey() + " 分数:" + entry.getValue());
    }
    }
    }
    }

    //对于Map集合按照Value值排序
    public List<Map.Entry<Integer, Integer>> sortMapByValue(Map<Integer, Integer> map) {
    // 将Map转化为List集合,List采用ArrayList
    List<Map.Entry<Integer, Integer>> list_Data = new ArrayList <Map.Entry<Integer, Integer>>(map.entrySet());
    // 通过Collections.sort(List I,Comparator c)方法进行排序
    Collections.sort(list_Data, new Comparator<Map.Entry<Integer, Integer>>() {

    @Override
    public int compare(Entry<Integer, Integer> o1, Entry<Integer, Integer> o2) {
    return (o2.getValue() - o1.getValue());
    }
    });
    return list_Data;
    }
    }
  • 相关阅读:
    table变宽格式
    IE11兼容性设定
    Spring AOP注解失效的坑及JDK动态代理
    关于何时执行shiro AuthorizingRealm 里的 doGetAuthenticationInfo与doGetAuthorizationInfo
    后端接收json数据交互
    关于JavaDate数据返回到前端变数字的问题(并引申到前后端时间的传输)
    git 列出两个分支 或者两个提交版本之间不同的文件名字
    map put相同的key
    MyBatis 中如何调用 Java 的 enum (枚举) 字段
    @ResponseBody 和 @RequestBody 的作用
  • 原文地址:https://www.cnblogs.com/sam-cheng/p/6284673.html
Copyright © 2011-2022 走看看