- package com.heima.string;
- public class Test2 {
- //统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数,其他字符出现的次数。ABCDEabcd123456!@#$%^
- public static void main(String[] args) {
- String s = " ABCDEabcd123456!@#$%^";
- int daxie = 0 ;
- int xiaoxie = 0;
- int shuzi = 0;
- int other = 0;
- for (int i = 0 ; i < s.length(); i ++){
- char c = s.charAt(i);
- if (c >= 'A' && c <= 'Z') {
- daxie ++;
- }else if (c >= 'a'&& c <= 'z'){
- xiaoxie ++;
- }else if (c>='0'&& c<='9') {
- shuzi ++;
- }else {
- other ++;
- }
- }
- System.out.println(s + "中大写字母有:" + daxie + "个,小写字母有:" + xiaoxie + "个,数字字符:"
- + shuzi + "个,其他字符:" + other + "个");
- }
- }
复制代码 |
|