1 import java.util.HashMap; 2 import java.util.Scanner; 3 4 /* 5 定义一个HashMap集合,并加入23、56、98、7四个元素,求总和及平均值 6 */ 7 public class Demo { 8 public static void main(String[] args){ 9 Scanner sc = new Scanner(System.in); 10 HashMap<Integer, Integer> hashMap = new HashMap<>(); 11 hashMap.put(1,23); 12 hashMap.put(2,56); 13 hashMap.put(3,98); 14 hashMap.put(4,7); 15 int sum = hashMap.get(1)+hashMap.get(2)+hashMap.get(3)+hashMap.get(4); 16 System.out.println("总和为:" + sum); 17 int avg = sum / 4 ; 18 System.out.println("平均值为:" + avg); 19 } 20 }