package java算法; import java.util.Scanner; /* * 题目:输入一行字符,分别统计出其英文字母,空格,数字和其他字符的个数 */ public class strIdentily { public static void main(String[] args) { // TODO Auto-generated method stub int abcCount=0; int spaceCount=0; int numCount=0; int otherCount=0; System.out.println("请输入一串字符:"); Scanner sc=new Scanner(System.in); String str=sc.nextLine(); char[]ch=str.toCharArray(); for(int i=0;i<ch.length;i++){ if(Character.isDigit(ch[i])){ numCount++; } else if(Character.isSpaceChar(ch[i])){ spaceCount++; } else if(Character.isLetter(ch[i])){ abcCount++; } else{ otherCount++; } } System.out.println("字母的个数:"+abcCount); System.out.println("数字的个数:"+numCount); System.out.println("空格的个数:"+spaceCount); System.out.println("其他字符的个数:"+otherCount); } }