键盘录入一个字符串,统计该字符串中的大写字母、小写字母、数字字符和其他字符分别有多少个 例如,键盘录入abcABCD12345!@#$%&,输出结果为:小写字母有3个,大写字母有4个,数字字符有5个,其他字符有6个。
代码:
public class TongJi {
public static void main(String[] args) {
//键盘录入字符串
Scanner sc = new Scanner(System.in);
System.out.println("请输入:");
String s = sc.nextLine();
int a = 0;
int b = 0;
int c = 0;
int d = 0;
//把字符串装换成对应的码表值进行处理
byte[] bt = s.getBytes();
for (int i = 0; i < bt.length; i++) {
if (bt[i]>=97&&bt[i]<=122) {
a++;
}else if (bt[i]>=65&&bt[i]<=90) {
b++;
}else if (bt[i]>=48&&bt[i]<=57) {
c++;
}else {
d++;
}
}
System.out.println("小写字母有"+a+"个,大写字母有"+b+"个,数字字符有"+c+"个,其他字符有"+d+"个 ");
}
}
示例: