为了账号安全,请及时绑定邮箱和手机立即绑定

java练习L7

java练习L7

狼顾之相1995 2016-09-24 08:20:28
题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。程序分析:利用while语句,条件为输入的字符不为'\n'.
查看完整描述

1 回答

?
JustWannaHugU

TA贡献452条经验 获得超796个赞

//C语言:
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;     
}
//java版本:
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.,/");
    }
}


查看完整回答
反对 回复 2016-09-24
  • 1 回答
  • 0 关注
  • 1117 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信