A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

* 键盘录入一段字符串,要求使用map集合统计出字符串中字母和数字出现的次数,如果有其他字符则当做*号来统计,最后按指定方式输出(不要求排序)
         * 例如录入字符串:aaaabbbcccddd1112233^^^
         * 输出的格式为: *(3),1(3),2(2),3(2),a(4),b(3),c(3),d(3) (注意:括号前面是字符,里面是次数)

1 个回复

倒序浏览
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

/**
* 题目二: 键盘录入一段字符串,要求使用map集合统计出字符串中字母和数字出现的次数,如果有其他字符则当做*号来统计,最后按指定方式输出(不要求排序)
* 例如录入字符串:aaaabbbcccddd1112233^^^ 输出的格式为:
* *(3),1(3),2(2),3(2),a(4),b(3),c(3),d(3) (注意:括号前面是字符,里面是次数)
* */
public class Day20_6点招题一2 {

        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                String str = sc.nextLine();

                printMap(stringToMap(str));
                System.out.println("\n==================");
                printMap(count(str));
                System.out.println("\n==================");
                getCharTimes(str);
        }

        // 方法一
        public static void getCharTimes(String str) {
                int times = 0;
                int indext = 0;

                String temp1 = str;
                for (int i = 0; i < str.length(); i++) {
                        String a = str.substring(i, i + 1);
                        if (a.matches("\\W") && (indext = temp1.indexOf(a)) != -1) {
                                times++;
                                temp1 = temp1.substring(indext + 1);
                        }
                }
                System.out.print("*" + "(" + times + "),");
                for (int i = 0; i < str.length(); i++) {
                        String temp = str.substring(0, 1);
                        if (temp.matches("\\W") || temp.matches("\\*")) {
                                str = str.replaceAll(temp, "");
                        } else {
                                int a = str.length();
                                str = str.replaceAll(temp, "");
                                int b = str.length();
                                System.out.print(temp + "(" + (a - b) + "),");
                        }
                }

        }

        // 方法二
        public static TreeMap<String, Integer> count(String str) {
                TreeMap<String, Integer> map = new TreeMap<String, Integer>();
                String temp = "";
                int times = 0;
                for (int i = 0; i < str.length(); i++) {
                        temp = str.substring(i, i + 1);
                        if (temp.matches("\\W")) {
                                times++;
                        } else {
                                if (map.size() == 0) {
                                        map.put(temp, 1);
                                } else {
                                        if (map.get(temp) == null) {
                                                map.put(temp, 1);
                                        } else {
                                                int n = map.get(temp);
                                                n++;
                                                map.put(temp, n);
                                        }
                                }
                        }
                        map.put("*", times);
                }
                return map;
        }

        // 方法三
        public static TreeMap<Character, Integer> stringToMap(String str) {
                char[] a = str.toCharArray();
                TreeMap<Character, Integer> map = new TreeMap<Character, Integer>();
                int times = 0;
                for (int i = 0; i < a.length; i++) {
                        if (map.containsKey(a[i])) {
                                if (!Character.toString(a[i]).matches("\\W")) {
                                        Integer in = map.get(a[i]);
                                        in++;
                                        map.put(a[i], in);
                                } else {
                                        times++;
                                        map.put('*', times);
                                }
                        } else {
                                if (Character.toString(a[i]).matches("\\W")) {
                                        times++;
                                        map.put('*', times);
                                } else {
                                        map.put(a[i], 1);
                                }
                        }
                }
                return map;
        }

        public static void printMap(TreeMap<Character, Integer> map) {
                int index = 0;
                for (Character key : map.keySet()) {
                        index++;
                        if (index == map.size()) {
                                System.out.print(key + "(" + map.get(key) + ")");
                        } else {
                                System.out.print(key + "(" + map.get(key) + "),");
                        }
                }
        }

        public static void printMap(Map<String, Integer> map) {
                int index = 0;
                for (String key : map.keySet()) {
                        index++;
                        if (index == map.size()) {
                                System.out.print(key + "(" + map.get(key) + ")");
                        } else {
                                System.out.print(key + "(" + map.get(key) + "),");
                        }
                }
        }
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马