- 题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
public class _007CountStringAll {
public static void main(String[] args) {
printCount();
}
private static void printCount() {
while (true) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一组字符 : ");
String string = scanner.nextLine();
char[] ch = string.toCharArray();
count(ch);
}
}
private static void count(char[] ch) {
int adbCount = 0;
int spaceCount = 0;
int numCount = 0;
int otherCount = 0;
for (int i = 0; i < ch.length; i++) {
// 判断是否是字母
if (Character.isLetter(ch[i])) {
adbCount++;
} //判断是否是数字
else if (Character.isDigit(ch[i])) {
numCount++;
} else if (Character.isSpaceChar(ch[i])) {
spaceCount++;
} else {
otherCount++;
}
}
System.out.println("字母个数是:" + adbCount);
System.out.println("数字个数是:" + spaceCount);
System.out.println("空格个数是:" + numCount);
System.out.println("其他字符个数是:" + otherCount);
}
}
网友评论