日期:2014-05-20  浏览次数:20779 次

书上题目 04-14
要求:输入一串字符,分别统计英文字母,数字,空格和其他字符的个数。(最好用DO-WHILE结构)
这是我写的代码:
import java.io.*;

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;
System.out.println("please input string");
do
{
ch=(char)System.in.read();
if(ch>='A'&&ch<='Z'||ch>='a'&&ch<='z'){
m+=1;}
else if(ch>='0'&&ch<='9'){
n+=1;}
else if(ch==' '){
o+=1;}
else {
p+=1;}

}while(ch!='\n');
System.out.println("英语字母有"+m+"个");
System.out.println("数字有"+n+"个");
System.out.println("空格有"+o+"个");
System.out.println("其他字符有"+p+"个");


}
}
问题:统计其他字符的个数总是不对,如何判断是不是特殊字符呢?
请各位指点!有更好的代码页可以贴出来!

------解决方案--------------------
Java code

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);

    }
}