1 package demo; 2 import java.util.Scanner; 3 /** 4 * 统计一个字符串中英文字母、空格、数字和其它字符的个数 5 */ 6 public class Statistics1 { 7 public static void main(String[]args){ 8 int i; 9 int LetterCount = 0; 10 int SpaceCount = 0; 11 int NumberCount = 0; 12 int OtherCount = 0; 13 14 //输入一个字符串 15 Scanner in = new Scanner(System.in); 16 System.out.println("请输入一个字符串:"); 17 String str = in.nextLine(); 18 19 //字符串转换成字符数组 20 char[]ch = str.toCharArray(); 21 for(i = 0; i<str.length();i++){ 22 if(Character.isLetter(ch[i])){ 23 LetterCount++; 24 }else if(Character.isSpaceChar(ch[i])){ 25 SpaceCount++; 26 }else if(Character.isDigit(ch[i])){ 27 NumberCount++; 28 } 29 else{ 30 OtherCount++; 31 } 32 } 33 34 System.out.println("字符的个数为:"+LetterCount); 35 System.out.println("空格的个数为:"+SpaceCount); 36 System.out.println("数字的个数为:"+NumberCount); 37 System.out.println("其他字符个数为:"+OtherCount); 38 } 39 }