本帖最后由 zfan 于 2014-5-2 04:26 编辑
- import java.io.*;
- import java.util.*;
- public class Test01 {
- /**
- * @param args
- */
- public static void main(String[] args){
- // TODO Auto-generated method stub ;
- System.out.println("请输入需要统计的字符:");
- ArrayList<Integer> StringStore = new ArrayList<Integer>();
- StringStore = StringStore();
- StringJudge(StringStore);
- }
- //用容器存储输入的字符;
- private static ArrayList<Integer> StringStore()
- {
- InputStream in = System.in;
- ArrayList<Integer> StringStore = new ArrayList<Integer>();
- try{
- while(true){
- int ch = in.read();
- if(ch=='\r')
- break;
- else
- StringStore.add(ch);
- }
- in.close();
- }
- catch(Exception e){
- }
- return StringStore;
- }
- //判断字符串中各类字符的数量并在控制台中输出;
- private static void StringJudge(ArrayList<Integer> StringStore)
- {
- int EngNum = 0;
- int SpaceNum = 0;
- int NumberNum = 0;
- int OthersNum = 0;
-
- for(int i=0;i<StringStore.size();i++){
- if((StringStore.get(i)>='a'&&StringStore.get(i)<='z')||(StringStore.get(i)>='A'&&StringStore.get(i)<='Z'))
- EngNum++;
- else if(StringStore.get(i)>='0'&&StringStore.get(i)<='9')
- NumberNum++;
- else if(StringStore.get(i)==' ')
- SpaceNum++;
- else
- OthersNum++;
- }
- System.out.println("英文字符的个数为"+EngNum);
- System.out.println("空格的个数为"+SpaceNum);
- System.out.println("数字的个数为"+NumberNum);
- System.out.println("其他字符的个数为"+OthersNum);
- }
- }
复制代码 |