zoukankan      html  css  js  c++  java
  • java 判断字符串中 大小写字母 数字和其他字符个数方法

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import qian.com.PrinTool;
     
    //定义一个判断字符串中 大写字母 小写字母  数字和其他字符的接口,以便规范其他类的判断字符串所含各种字符个数;
    interface DecideNum{
    public void decideNum(String str);
    }
     
    public class StatDemo implements DecideNum {
    public void decideNum(String str){
    int upperNum = 0;
    int lowerNum = 0;
    int intNum = 0;
    int otherNum = 0;
    for(int i =0;i
    //判断大写字母
    if((int)str.charAt(i)>64 && (int)str.charAt(i)<91){
    upperNum = upperNum+1;
    }
    //判断小写字母
    else if((int)str.charAt(i)>96 && (int)str.charAt(i)<123){
    lowerNum = lowerNum+1;
    }
    //判断数字
    else if((int)str.charAt(i)>47 && (int)str.charAt(i)<58){
    intNum = intNum+1;
    }
    //判断其他字符个数
    else{
    otherNum = otherNum+1;
    }
    }
    PrinTool.pri("大写字母个数为:    "+upperNum);
    PrinTool.pri("小写字母个数为:    "+lowerNum);
    PrinTool.pri("数字个数为:    "+intNum);
    PrinTool.pri("其他字符个数为:    "+otherNum);
    }
    public static void main(String [] args) throws IOException{
    String st;
    //接受键盘输入:
    System.out.print("请输入要判断的字符串:");
    BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
    st = br.readLine();
    StatDemo stat =new StatDemo();
    stat.decideNum(st);
    }
    }
    本文在学到了策略设计模式后,又将这个方法改成了策略设计模式(见另一篇文章),虽然繁琐了许多,只在表达策略设计模式的思想。。
  • 相关阅读:
    JS 百度地图路书---动态路线
    jQuery---创建和添加节点
    CSS基础
    第一篇:前端知识之HTML内容
    JS高级---为内置对象添加原型方法
    JS DOM属性+JS事件
    Vue-router
    vue使用kkfileview文件预览功能
    JS高级---案例:验证密码的强度
    promise是怎么来的?
  • 原文地址:https://www.cnblogs.com/hd92/p/13553734.html
Copyright © 2011-2022 走看看