A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 代码人生?! 中级黑马   /  2016-5-5 20:08  /  458 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

分析以下需求,并用代码实现:
        (1)从键盘录入一个字符串
        (2)统计该串中有大写字母、小写字母、数字各有多少个。
                举例:
                        Hello12345World
                        大写 : 2个
                        小写 : 8个
                        数字 : 5个
package com.heima.test;
import java.util.Scanner;
public class Day12_Work2 {

        /**
         * @param args
         */
        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                String s = sc.nextLine();
                int big = 0;
                int small = 0;
                int num = 0;
                int other = 0;
                for (int i = 0; i < s.length(); i++) {
                        char c = s.charAt(i);
                        if (c>='A'&&c<='Z'){
                                big++;
                        }else if(c>='a'&&c<='z'){
                                small++;
                        }else if(c>='1'&&c<='9'){
                                num++;
                        }else{
                                other++;
                        }
                }
                System.out.println("大写:"+big+"个");
                System.out.println("小写:"+small+"个");
                System.out.println("数字:"+num+"个");
        }
}
       
您需要登录后才可以回帖 登录 | 加入黑马