输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。请教下各方高手,我还是个JAVA初学者,麻烦大家了

解决方案 »

  1.   

    public class ZhangZhenWei
    {
    public static void main(String [] args)
    {
    String s = "zhangzhenwei";
    int number = 0,smallchar = 0,bigchar = 0,other = 0;
    for(int i = 0;i < s.length();i++)
    {
    char n = s.charAt(i);
    if(n>='0'&&n<='9') number++;
    else if (n>='a'&&n<='z') smallchar++;
    else if (n>='A'&&n<='Z') bigchar++;
    else other++;
    }
    }
    }//输出我就不输出了,我也是初学者有问题我们可以讨论。
      

  2.   

    我来一个全的
    package com.fafeiboy.test;public class TestCount {
    private int i = 0; private int j = 0; private int k = 0; private int l = 0; private int m = 0; public void countChar(String s) {
    char[] arrayChar = s.toCharArray();
    char c;
    for (int x = 0; x < arrayChar.length; x++) {
    c = arrayChar[x];
    if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
    //判断英文字母
    i++;
    } else if (c >= '\u4e00' && c <= '\u9fa5') {
    //判断中文
    j++;
    } else if (c == ' ') {
    //判断空格
    k++;
    } else if (c >= '0' && c < '9') {
    //判断数字
    l++;
    } else {
    m++;
    }
    }
    System.out.println("i:" + i);
    System.out.println("j:" + j);
    System.out.println("k:" + k);
    System.out.println("l:" + l);
    System.out.println("m:" + m);
    }

    public static void main(String[] args){
    TestCount tc = new TestCount();
    tc.countChar("asdfasdf中   a  121 \\[][23423.,/");
    }
    }
      

  3.   

    int main(){
         char temp;
         while(getchar(temp)!='\n'){
              if(number range){
                   numberNum++;
              }else if(charator range){
                   charactorNum++;
              }else if(space){
                   spaceNum++;
              }else {
                   otherNum++;
              }
         }
         printf("Number:%d\nCharactor:%d\nSpace::%d\nOther:%d\n");
         return 0;     
    }
      

  4.   

    2楼的是答案,不过这个问题学C语言基础的时候相信大家都做过,算法还是那样。不过是用了java的特有的对象表现出来了,呵呵···