资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

统计字符个数java代码 Java统计字符个数

java统计串中字符个数的代码解释

整个代码就是为了统计字符串中各个字符出现的个数

成都创新互联公司-专业网站定制、快速模板网站建设、高性价比邢台县网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式邢台县网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖邢台县地区。费用合理售后完善,十年实体公司更值得信赖。

MapCharacter, Integer result = getCharMaps(s); 这个方法调用

主要是看这个方法

public static MapCharacter, Integer getCharMaps(String s) {

MapCharacter, Integer map = new HashMapCharacter, Integer(); //定义一个map

for(int i = 0; i s.length(); i++) { // 下面的代码主要是循环字符串,一次取出每一个字符

Character c = s.charAt(i); //获取对应的字符

Integer count = map.get(c); //获取字符对应出现的次数

map.put(c, count == null ? 1 : count + 1); //如果之前出现过,则加1,否则直接赋1

}

return map; //返回最终的map

}

利用Java语言代码输入一行字符分别统计其中英文字母、空格、数字和其他字符的个数。

123456789101112131415161718192021222324

public static void main(String[] args) throws IOException {        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));        String str=br.readLine();         int countNum = 0;//统计数字的个数        int countChar = 0;//统计英文字母的个数        int countSpace = 0;//统计空格的个数        int countOthers = 0;//统计其它字符的个数        for (int i = 0; i  str.length(); i++) {            char c = str.charAt(i);            if (c = '0'  (int) c = '9') {                countNum++;            } else if ((c = 'a'  c = 'z')||(c = 'A'  c = 'Z')) {                countChar++;            } else if (c == ' ') {                countSpace++;            } else{                countOthers++;            }        }        System.out.println("数字个数:"+countNum);        System.out.println("英文字母个数:"+countChar);        System.out.println("空格个数:"+countSpace);        System.out.println("其他字符个数:"+countOthers);    }

一、问题分析:

输入一行字母,那么会以换行结束。所以可以存入数组,也可以逐个输入,遇到换行结束。

要统计各个类的个数,就要逐个判断是哪个分类的。

由于在ASCII码中,数字,大写字母,小写字母分别连续,所以可以根据边界值判断类型。

二、算法设计:

1、读入字符,直到遇到换行结束。

2、对于每个字符,判断是字母还是数字,或者空格,或者是其它字符。

3、对于每个字符判断后,对应类别计数器自加。

4、最终输出结果。

java怎么统计一个字符的个数字

int i;

06

/***countDigit统计数字的数目

07

* countLetter统计字母的数目

08

* countLetter统计其余的数目

09

*/

10

int countDigit=0, countLetter=0, countOthers=0;

11

12

String input = “dfdfdf23dfdjk8989”;

13

14

//将字符串变量转化为字符数组

15

char[] charArray = input.toCharArray();

16

for(i=0;icharArray.length;i++)

17

{

18

//ASIIC码

19

if(charArray[i]='z'charArray[i]='a' || charArray[i]='Z'charArray[i]='A')

20

countLetter++;

21

else if(charArray[i]='9' charArray[i]='0' )

22

countDigit++;

23

else

24

countOthers++;

25

}

26

System.out.println("the number of letter "+countLetter);

27

System.out.println("the number of digit "+countDigit);

编写java程序统计字符个数。

public static void main(String[] args) {

// TODO Auto-generated method stub

int abcCount=0;//英文字母个数

int spaceCount=0;//空格键个数

int numCount=0;//数字个数

int otherCount=0;//其他字符个数

Scanner scan=new Scanner(System.in);

String str=scan.nextLine();

char[] ch = str.toCharArray();

for(int i=0;ich.length;i++){

if(Character.isLetter(ch[i])){

//判断是否字母

abcCount++;

}

else if(Character.isDigit(ch[i])){

//判断是否数字

numCount++;

}

else if(Character.isSpaceChar(ch[i])){

//判断是否空格键

spaceCount++;

}

else{

//以上都不是则认为是其他字符

otherCount++;

}

}

System.out.println("字母个数:"+abcCount);

System.out.println("数字个数:"+numCount);

System.out.println("空格个数:"+spaceCount);

System.out.println("其他字符个数:"+otherCount);


网站栏目:统计字符个数java代码 Java统计字符个数
分享地址:http://cdkjz.cn/article/hjgocc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220