本帖最后由 黑夜中那颗星 于 2015-10-26 23:59 编辑
- import java.io.*;
- import java.util.*;
- public class Test {
- public static void main(String[] args) throws IOException{
- System.out.println(demo());
- }
- public static String demo() throws IOException{
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- HashMap<String,Integer> hm = new HashMap<String,Integer>();
- StringBuilder sb = new StringBuilder();
- String value = br.readLine();
- char[] ch = value.toCharArray();
- hm.put("upper",0); //大写
- hm.put("lower",0); //小写
- hm.put("number",0); //数字
- hm.put("other",0); //其他
- for(int x = 0;x<ch.length;x++){
- if(ch[x]>='A'&&ch[x]<='Z'){ //大写计数
- int num = hm.get("upper");
- hm.put("upper",++num);
- }
- else if(ch[x]>='a'&&ch[x]<='z'){//小写计数
- int num = hm.get("lower");
- hm.put("lower",++num);
- }
- else if(ch[x]>='0'&&ch[x]<='9'){//数字计数
- int num = hm.get("number");
- hm.put("number",++num);
- }
- else{
- int num = hm.get("other");//其他计数
- hm.put("other",++num);
- }
- }
- Set<Map.Entry<String, Integer>> set = hm.entrySet();
- Iterator<Map.Entry<String, Integer>> it = set.iterator();
- while(it.hasNext()){
- Map.Entry<String, Integer> map = it.next();
- String k = map.getKey();
- int v = map.getValue();
- sb.append(k+"("+v+")");
- }
- return sb.toString();
- }
- }
复制代码
|