输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

package lianxi;
import java.util.*;
public class Zifugeshu {

    public static void main(String[] args) {
    System.out.println("输入一个字符串");
    Scanner shuru=new Scanner(System.in);
    String s=shuru.nextLine();
    String zimu="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
           kongge=" ",shuzi="0123456789";
    int sum1=0,sum2=0,sum3=0;
    
    for (int i = 0; i < s.length(); i++)
    {
        for (int j = 0; j < zimu.length(); j++) 
        {
            if(s.substring(i,i+1).equals(zimu.substring(j, j+1)))
            {
                sum1=sum1+1;
break; } } }
for (int i = 0; i < s.length(); i++) { if(s.substring(i,i+1).equals(kongge)) { sum2=sum2+1; } } for (int i = 0; i < s.length(); i++) { for (int j = 0; j < shuzi.length(); j++) { if(s.substring(i,i+1).equals(shuzi.substring(j, j+1))) { sum3=sum3+1;
break; } } } System.out.println(
"英文字母个数: "+sum1); System.out.println("空格个数: "+sum2); System.out.println("数字个数: "+sum3); System.out.println("其他字符个数: "+(s.length()-sum1-sum2-sum3)); } }

posted @ 2016-05-14 15:55  凌零聆  阅读(1469)  评论(0编辑  收藏  举报