输入一行字符,分别统计出其中英文字母、数字、空格和其他字符的个数。

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int digital = 0, character = 0,blank = 0,other = 0;
char[] ch = null;
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();           //表示对数据的读取,一次性读取一行。用String接收后可进行相应处理。
ch = s.toCharArray();   //:将字符串对象中的字符转换为一个字符数组
for (int i = 0; i < ch.length; i++) {  //循环数组元素
if((ch[i]>='a'&&ch[i]<='z')||(ch[i]>='A'&&ch[i]<='Z')){  //判断是否是字符
character ++;
}
else if(ch[i]>='0'&&ch[i]<='9'){  //判断是否是数字
digital ++;
}
else if(ch[i]==' '){  //判断空格
blank ++;
}else{  //其他
other ++;
}
}
System.out.println(character+" "+digital+" "+blank+" "+other);
}
}

猜你喜欢

转载自blog.csdn.net/qq_39822872/article/details/83212024