java练习4
1 //题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 2 //"slbaTINDNF s 32423 2345$p0as0@sdlf&s " 3 System.out.println("请输入一个字符串:"); 4 String str=sc.nextLine(); 5 int countN=0; 6 int countC=0; 7 int countS=0; 8 int others=0; 9 for(int i=0;i<str.length();i++) 10 { 11 char c=str.charAt(i); 12 if(c>='0'&&c<='9') 13 { 14 countN++; 15 }else if ((c >= 'a' && c <= 'z')||(c >= 'A' && c <= 'Z')) 16 { 17 countC++; 18 }else if(c==' ') 19 { 20 countS++; 21 }else 22 { 23 others++; 24 } 25 26 27 28 } 29 30 System.out.println("有"+countN+"个数字"); 31 System.out.println("有"+countC+"个字母"); 32 System.out.println("有"+countS+"个空格"); 33 System.out.println("有"+others+"个其它字符"); 34 35
运行结果: