黑马程序员技术交流社区

标题: 统计大小写字符个数 [打印本页]

作者: Over_Mstuang    时间: 2015-8-12 18:27
标题: 统计大小写字符个数
package cn.itcast.chapterll;
//import Java.util.Scanner;
/*
* 统计大写小写字符的个数
*
* 举例:
*                 Hello12345World
*
*                 大写:2
*                 小写:8
*                 数字:5
*
* 思路:
*                 A:定义三个统计变量
*                 B:获取到每一个字符。遍历字符串。
*                 C:判断是属于哪种范围的
*                         大写:65-90
*                         小写:97-122
*                         数字:48-57
*                C:是哪种哪种++
*/
public class HelloWroldTest {
        public static void main(String[] args) {
                String s = "Hello12345Wrold";
                               
                                //                 A:定义三个统计变量
                                int bigCount = 0;
                        int smallCount = 0 ;
                                int numberCount = 0;
                               
                                for (int x = 0; x < s.length(); x++) {
                                        char ch = s.charAt(x);

                                if (ch >= 'A'&&ch<='Z') {bigCount ++;
                                       
                                }else if (ch>='a'&&ch <= 'z') {smallCount++;
                               
                                       
                                }else if (ch>='0'&&ch<='9') {numberCount++;
                               
                                       
                                }
                                else break;
               
                                }
                                System.out.println("大写:"+bigCount);
                                System.out.println("小写:"+smallCount);
                                System.out.println("数字:"+numberCount);
        }

}

作者: 思考。。。    时间: 2015-8-12 18:40
多谢分享~




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2