黑马程序员技术交流社区
标题:
统计一串字符中的个数
[打印本页]
作者:
792241113
时间:
2016-10-13 02:38
标题:
统计一串字符中的个数
public class Test3 {
// 题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
// 程序分析:利用while语句,条件为输入的字符不为'\n'.
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
System.out.println("请输入需要检验的字符:");
String s = sc.nextLine();
method(s);
}
private static void method(String s) {
int D=0;
int X =0;
int k=0;
int num=0;
int l=0;
char [] ch =s.toCharArray();
for (char c :ch) {
if (c>='0' && c<='9'){
num++;
}else if(c==' '){
k++;
}else if(c>='A' && c<='Z'){
D++;
}else if(c>='a' && c<='z'){
X++;
}else{
l++;
}
}
System.out.println("数字:"+num+" 空格"+k+" 大写"+D+" 小写"+X+" 乱七八糟"+l);
}
}
作者:
韦晨旭
时间:
2016-10-13 07:49
厉害厉害
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2