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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 果蝇 中级黑马   /  2015-9-5 23:04  /  143 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

A:案例演示
                需求:遍历字符串
                WeAreTheBestWeCanDoIt  -- chatAt(i) 自己做
                                                           -- toCharArray(); 自己做
               
B:案例演示
                需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)
               
                String s  = "WeA2354reTheBest43542WeC76anDoIt";
                分析:
                0.求什么就定义什么 的思想
                        定义三个 :大 小  数字  count
                1.遍历字符串
                2.获取的到每一个字符,判断字符的范围  
                3.count++
               
               
               
               
*/

public class StringTest {
        public static void main(String[] args) {
                String s  = "WeeeA23354reTheBest43542WeC76anDoIt";
                /*
                 * 大写字母的个数8
                        小写字母的个数13
                        数字的个数11
                 *
                 */
               
                //定义三个 count
               
                int countB = 0;
                int countS = 0;
                int countN = 0;
                //遍历
                for (int i = 0; i < s.length(); i++) {
                        // System.out.println(s.charAt(i));
                        char ch = s.charAt(i);
                        //判断
                        if (ch >='A' && ch<='Z') {  //char 基本类型   
                                //大写
                                countB++;
                        } else if(ch >='a'&& ch<='z')  {
                                countS++;

                        }else {
                                countN++;
                               
                        }
                       
                }
               
                System.out.println("大写字母的个数" + countB);
                System.out.println("小写字母的个数" + countS);
                System.out.println("数字的个数" + countN);
        }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马