可统计你录入的数字,字母,空格,符号。
package com.baidu.zhidao;
import java.util.Scanner;
public class CharacterStatistics {
public static void main(String[] args) {
while (true) {
try {
int numberCount = 0,charCount = 0,spaceCount = 0,otherCount = 0;
System.out.println("Waiting for typing...");
Scanner s = new Scanner(System.in);
String str = s.nextLine();
char[] ch = str.toCharArray();
for (char c : ch) {
if(c>='0'&&c<='9'){
numberCount++;
}else if(c>='a'&&c<='z'||c>='A'&&c<='Z'){
charCount++;
}else if(c == ' '){
spaceCount++;
}else{
otherCount++;
}
}
System.out.println("Amount of Number: " + numberCount);
System.out.println("Amount of Letters: " + charCount);
System.out.println("Amount of Space: " + spaceCount);
System.out.println("Amount of Characters: " + otherCount);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}