public void work5() {
int letter = 0;
int upCaseCnt = 0;
int lowCaseCnt = 0;
int spaceCnt = 0;
int numCnt = 0;
int otherCnt = 0;
System.out.println("请输入字符串:");
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();f
// int len = str.length();
// for (int i = 0; i < len; i++) {
// char c = str.charAt(i);
// if (c >= 'A' && c <= 'Z')
// upCaseCnt++;
// else if (c >= 'a' && c <= 'z')
// lowCaseCnt++;
// else if (c >= '0' && c <= '9')
// numCnt++;
// else if (c == ' ')
// spaceCnt++;
// else otherCnt++;
// }
char[] array = str.toCharArray();
for (int i = 0; i < array.length; i++) {
if ((array[i] >= 'a' && array[i] <= 'z') ||
(array[i] >= 'A' && array[i] <= 'Z')) {
letter++;
}
if (array[i] >= 'A' && array[i] <= 'Z')
upCaseCnt++;
else if (array[i] >= 'a' && array[i] <= 'z')
lowCaseCnt++;
else if (array[i] >= '0' && array[i] <= '9')
numCnt++;
else if (array[i] == ' ')
spaceCnt++;
else otherCnt++;
}
System.out.println(array);
System.out.println("字母:" + letter);
System.out.println("大写字母:" + upCaseCnt);
System.out.println("小写字母:" + lowCaseCnt);
System.out.println("空格:" + spaceCnt);
System.out.println("数字:" + numCnt);
System.out.println("其他:" + otherCnt);
}
记录输入字符串中大写字母,小写字母,空格,数字等出现的次数
最新推荐文章于 2024-04-27 09:13:51 发布