import java.util.Scanner;
/**
* 从键盘输入一个字符串,统计其中各种字符的个数(大写字母、小写字母、数字、其他)
*/
public class StringTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入一个字符串:");
String input = sc.next();
sc.close();
int countUpper = 0;
int countLower = 0;
int countNumber = 0;
int countOther = 0;
char[] arr = input.toCharArray();
for (char x : arr) {
if (x >= 'A' && x <= 'Z') {
countUpper++;
} else if (x >= 'a' && x <= 'z') {
countLower++;
} else if (x >= '0' && x <= '9') {
countNumber++;
} else {
countOther++;
}
}
System.out.println("大写字母个数" + countUpper);
System.out.println("小写字母个数" + countLower);
System.out.println("数字个数" + countNumber);
System.out.println("其他字符个数" + countOther);
}
}
输入输出示例:
请输入一个字符串:Hello2020August!
大写字母个数2
小写字母个数9
数字个数4
其他字符个数1