- package statistics;
- import java.util.Scanner;
- public class Statistics {
- public static void statistics() {
- // 创建对象键盘录入
- Scanner sc = new Scanner(System.in);
- // 获取String类型的数据
- String str = sc.nextLine();
- // 将录入字符串转换成字符数组
- char[] str1 = str.toCharArray();
- // 定义四个变量记录大写、小写、数字、汉子出现的个数
- int capitalCount = 0;
- int lowercaseCount = 0;
- int numCount = 0;
- int chinese = 0;
- // 遍历字符数组
- for (int i = 0; i < str1.length; i++) {
- // 进行判断比较来统计变量
- if (str1[i] >= 'A' && str1[i] <= 'Z') {
- capitalCount++;
- } else if (str1[i] >= 'a' && str1[i] <= 'z') {
- lowercaseCount++;
- } else if (str1[i] >= '0' && str1[i] <= '9') {
- numCount++;
- } else {
- chinese++;
- }
- }
- // 打印
- System.out.println("大写:" + capitalCount + "\n" + "小写:" + lowercaseCount
- + "\n" + "数字:" + numCount + "汉字:" + "\n" + chinese);
- }
- }
- [code]package statistics;
- public class StatisticsTest {
- public static void main(String[] args) {
- Statistics.statistics();
- }
- }
复制代码 [/code] |