ITPub博客

首页 > 应用开发 > Java > 统计字符串中包含各种字符的个数

统计字符串中包含各种字符的个数

原创 Java 作者:影中漫步 时间:2014-08-12 21:59:31 0 删除 编辑

有一个字符串,其中包含中文字符、英文字符和数字字符,请统计和打印出各个字符的个数。

------------
package com.neusoft.tttrtt;

import java.util.Scanner;

public class tongji {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
System.out.println("请输入一组字符串:");
String str = sc.next();
int shu   = 0;
int zimu  = 0;
int hanzi = 0;

for (int i = 0; i < str.length(); i++) {
if(str.charAt(i)>= '0'&& str.charAt(i)<='9'){
shu++;
}else if((str.charAt(i)>='a' && str.charAt(i)<='z' )||(str.charAt(i)>='A' && str.charAt(i)<='Z')){
zimu++;
}else if(str.charAt(i)>=19968 && str.charAt(i)<40623){
hanzi++;
}
}

System.out.println("数字的个数为:"+shu);
System.out.println("字母的个数为:"+zimu);
System.out.println("汉字的个数为:"+hanzi);

}

}
/*5、有一个字符串,其中包含中文字符、英文字符和数字字符,请统计和打印出各个字符的个数。*/



来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29667072/viewspace-1250146/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: ORACLE优化(一)
请登录后发表评论 登录
全部评论

注册时间:2014-05-19

  • 博文量
    15
  • 访问量
    26328