public class CharCountTest {
public static void main(String[] args) {
String str =JOptionPane.showInputDialog("输入字符串!");
System.out.println("输入的字符串:"+str);
int numCount = 0; //数字
int letterCapCount=0;//大写字母
int letterCount=0; //小写字母
int otherCount = 0; //其他字符
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c >= '0' && c <= '9') {
numCount++;
}
else if(c >= 'a' && c<='z')
{
letterCount++;
}
else if (c >= 'A' && c<='Z')
{
letterCapCount++;
}
else{
otherCount++;
}
}
System.out.println("数字个数:"+numCount);
System.out.println("小写字母个数:"+letterCount);
System.out.println("大写字母个数:"+letterCapCount);
System.out.println("其他个数:"+otherCount);
}
} |