题解 | #统计字符#

统计字符

http://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5

千万不要用ASCII码,会出错。最好直接比较。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
	public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = (br.readLine().trim());

        int Eng = 0;
        int blank =0;
        int num = 0;
        int other = 0;
        
        for(int i=0;i<str.length();i++) {
        	char ch = str.charAt(i);
        	if( ch>='a'&& ch<='z' || ch>='A'&&ch<='Z') Eng++;
        	else if(ch==' ') blank++;
        	else if(ch>='0'&& ch<'9') num++;
        	else other++;
        }
        
        System.out.println(Eng);
        System.out.println(blank);
        System.out.println(num);
        System.out.println(other);
	}
}
全部评论

相关推荐

评论
2
5
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务