日期:2014-05-20 浏览次数:21066 次
public class Count {
public static void main(String[] args) throws IOException {
(new CountChar()).count();
}
}
class CountChar {
public void count() throws IOException {
char ch;
int m = 0;
int n = 0;
int o = 0;
int p = 0;
Map<String,HashSet<Character>> map = new HashMap<String,HashSet<Character>>();
System.out.println("please input string");
do {
ch = (char) System.in.read();
if(ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z'){
if(map.containsKey("英文字母")){
map.get("英文字母").add(ch);
}
else{
HashSet<Character> chs = new HashSet<Character>();
chs.add(ch);
map.put("英文字母", chs);
}
m++;
} else if (ch >= '0' && ch <= '9'){
if(map.containsKey("数字")){
map.get("数字").add(ch);
}
else{
HashSet<Character> chs = new HashSet<Character>();
chs.add(ch);
map.put("数字", chs);
}
n += 1;
} else if (ch == ' ') {
if(map.containsKey("其他字符")){
map.get("其他字符").add(ch);
}
else{
HashSet<Character> chs = new HashSet<Character>();
chs.add(ch);
map.put("其他字符", chs);
}
o += 1;
}else{
if(map.containsKey("其他字符")){
map.get("其他字符").add(ch);
}
else{
HashSet<Character> chs = new HashSet<Character>();
chs.add(ch);
map.put("其他字符", chs);
}
System.out.println(ch=='\r'||ch=='\n');//你这里的其他字符就是这两个字符。没有统计错吧
p += 1;
}
} while (ch != '\n');
System.out.println("英语字母有" + m + "个");
System.out.println("数字有" + n + "个");
System.out.println("空格有" + o + "个");
System.out.println("其他字符有" + p + "个");
System.out.println(map);
}
}