实例代码二(HashMap和LinkedList的应用)

题目大致为某班级两次考试的成绩单, 有三名同学张三李四王五,有两次考试,用HashMap<String,Integer>存储考试成绩(key为姓名,value为成绩),用LinkedList<HashMap<String,Integer>>存储考试次数,一次考试一个HashMap。

问题: 查询某次考试的总成绩 查询某个学生的总成绩 查询某个学生的平均成绩

package com;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class EqualsTest {
   public static void main(String[] args) {
	  HashMap<String,Integer> map = new HashMap<String,Integer>();
	  map.put("张三", 80);
	  map.put("李四", 65);
	  map.put("王五", 35);
	  HashMap<String,Integer> map1 = new HashMap<String,Integer>();
	  map1.put("张三", 88);
	  map1.put("李四", 75);
	  map1.put("王五", 45);
	  LinkedList<HashMap<String,Integer>> list1 = new LinkedList<HashMap<String,Integer>>();
	  list1.add(map);
	  list1.add(map1);
	  Scanner sc = new Scanner(System.in); 
	  int count=0;
	  int a=sc.nextInt();
	  for(Iterator it=list1.get(a-1).values().iterator();it.hasNext();) {
			int num=(Integer)it.next();
			count+=num;
	  }
	  System.out.println("总成绩"+count);
	  
	  count=0;
	  String name = sc.next();
	  for(int i=0;i<list1.size();i++) {
		  Set<String> c=list1.get(i).keySet();
		  for(Iterator it=c.iterator();it.hasNext();) {
		  String s = (String) it.next();
		  if(s.equals(name)) {
			  int num1 = list1.get(i).get(s);
			  count+=num1;
		  }
		  }
		  c=null;
	  }
	  System.out.println(count);
	  System.out.println((double)count/list1.size());	  
}
}


猜你喜欢

转载自blog.csdn.net/weixin_40373090/article/details/80663849