java判断字符串中⼤⼩写字母数字和其他字符个数⽅法
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import qian.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);
}
}
本⽂在学到了策略设计模式后,⼜将这个⽅法改成了策略设计模式(见另⼀篇⽂章),虽然繁琐了许多,只在表达策略设计模式的思想。。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。