java--遍历字符串

package com.test;

public class zifuchuanbianli {
public static void main(String[] args) {
String s ="ABCDEabcdef123456!@#$%%^";
int big = 0;
int small = 0;
int num = 0;
int other = 0;
for(int i=0;i<s.length();i++) {
//System.out.println(s.charAt(i));
char c = s.charAt(i); //通过索引拿取每一个字符
//判断是否在此范围之内
if(c >= 'A' && c <= 'Z') {
big++; //如果满足大写,计数器递增
}else if(c >= 'a' && c <= 'z'){
small++;
}else if(c >= '0' && c <= '9') {
num++;
}else {
other++;
}
}
System.out.println("大写字母"+big);
System.out.println("小写字母"+small);
System.out.println("数字"+num);
System.out.println("其他"+other);
}
}

posted @ 2020-05-22 14:08  雨曼晴川  阅读(955)  评论(0编辑  收藏  举报