zoukankan      html  css  js  c++  java
  • Document

    前言:其实我还是有点不懂,有点郁闷了,算了直接把代码放上去把。

    方法一:

    Scanner input=new Scanner(System.in);
            System.out.println("请输入一个字符");
            String str=input.next();
            
            char[] strChar=str.toCharArray();
            //声明集合,把之存在集合中
            Map<Character,Integer> map=new HashMap<>();
            
            for(int i=0;i<strChar.length;i++){
                if(map.containsKey(strChar[i])){
                    map.put(strChar[i],map.get(strChar[i])+1);
                }else{
                    map.put(strChar[i],1);
                }
            }
            
            for(Map.Entry<Character, Integer> entry: map.entrySet()){
                System.out.println(entry.getKey()+":"+entry.getValue());
            }
            input.close();
    View Code

    方法二:

     1 Scanner input=new Scanner(System.in);
     2         System.out.println("请输入一个字符");
     3         String str=input.next();
     4         
     5         
     6         //声明集合,把之存在集合中
     7         Map<Character,Integer> map=new HashMap<>();
     8         
     9         for(int i=0;i<str.length();i++){
    10             if(map.containsKey(str.charAt(i))){//如果此映射将一个或多个键映射到指定值,则返回 true。
    11                 map.put(str.charAt(i),map.get(str.charAt(i))+1);
    12             }else{
    13                 map.put(str.charAt(i),1);
    14             }
    15         }
    16         
    17         for(Map.Entry<Character, Integer> entry: map.entrySet()){
    18             System.out.println(entry.getKey()+":"+entry.getValue());
    19         }
    20         input.close();
    View Code
  • 相关阅读:
    实时监听输入框值变化的完美方案:oninput & onpropertychange
    展示两行,超出用。。。表示
    修改下拉框滚动条样式
    js单线程工作
    锚点
    火狐图片乱码
    解决重复提交的几种方法
    forward和redirect的区别
    form表单刷新自动提交
    addEventListener和attachEvent的区别
  • 原文地址:https://www.cnblogs.com/chenyanlong/p/7845815.html
Copyright © 2011-2022 走看看