public class Test2 { public static void main(String[] args) { Random r = new Random(); // 选择 String[] ele = {"A","B","C","D","AB","AC","AD","ABC","ABD","ACD","ABCD","BC","BD","BCD","CD"}; // 姓名 String[] names = createNames(); printArr(names); // 1 要求程序记录每个学生的选择情况 // 使用一个Map集合存储 Mapdata = new TreeMap<>(); // 把学生选择的数据存入进去 for (int i = 0; i < names.length; i++) { String se = ele[r.nextInt(ele.length)]; data.put(names[i],se); } // 选择数量存储 Map infos = new HashMap<>(); // 遍历 Collection values = data.values(); // 遍历所有姓名的选择 for (String value : values) { // 遍历所有选择的内容 for (String s : values) { System.out.print(s+"t"); for (int i = 0 ; i < s.length();i++){ char ch = s.charAt(i); if (infos.containsKey(ch)){ infos.put(ch,infos.get(ch)+1); } else { infos.put(ch,1); } } } } System.out.println(); System.out.println(infos); } public static String[] createNames(){ String[] names = new String[3]; for (int i = 0; i < names.length ; i++) { names[i] = (i+1)+"号"; } return names; } public static void printArr(String[] arr){ StringBuilder sb = new StringBuilder('['); for (int i = 0; i < arr.length; i++) { sb.append(arr[i]); } sb.append(']'); System.out.println(sb); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)