题目:
键盘录入一个字符,统计字符串中的大小写字母及数字字符个数。
import java.util.Scanner;
public class Demo09 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String s = scanner.nextLine();
int bigCount = 0;
int smallCount = 0;
int numberCount = 0;
char[] c = s.toCharArray();
for (int i = 0; i < c.length; i++) {
char ch = s.charAt(i);
if (ch >= 'a' && ch <= 'z') {
smallCount++;
} else if (ch >= 'A' && ch < 'z') {
bigCount++;
} else if (ch >= '0' && ch <= '9') {
numberCount++;
} else {
System.out.println("该字符" + c[i] + "非法");
}
}
System.out.println("小写字母有" + smallCount + "个");
System.out.println("大写字母有" + bigCount + "个");
System.out.println("数字字符有" + numberCount + "个");
}
}
运行结果示例: