编程实现从键盘输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
import java.util.Scanner;
import static java.lang.System.out;
public class test {
public static void main(String args[]) {
out.println("请输入字符串");
Scanner scanner = new Scanner(System.in);
char ch[]=null;
// next()方法遇到空格的确会停止从键盘读数据的操作;
// nextLine()方法会自动读到第一次输入完后用户敲的回车(Enter键),从而导致第二次输入无法进行;
// toCharArray()把字符串转化成字符数组
String str = scanner.nextLine();
ch = str.toCharArray();
int number=0;
int character=0;
int blank=0;
int other=0;
for (int i = 0; i < ch.length; i++) {
if(ch[i]>'0'&&ch[i]<'9'){
number++;
}else if (ch[i]>'a'&&ch[i]<'z'){
character++;
}else if (ch[i]==' '){
blank++;
}else {
other++;
}
}
System.out.println(number);
System.out.println(character);
System.out.println(blank);
System.out.println(other);
}
}