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

public class Sum {
    
    
	public static void main(String[] args) {
    
    
		Scanner sc = new Scanner(System.in);
		String s = sc.nextLine();
		int a=0,b=0,c=0,d=0;
		char ch[] = s.toCharArray();
		int i=0;
		for(;i<ch.length;i++) {
    
    
			if((ch[i]>='a' && ch[i]<='z')||(ch[i]>='A'&&ch[i]<='Z'))
				a++;
			else if(ch[i]==' ')
				b++;
			else if(ch[i]>='0' && ch[i]<='9')
				c++;
			else
				d++;
		}
		System.out.println("英文字母:"+a+"\n空格:"+b+"\n数字"+c+"\n其他字符:"+d);
		
	}
}

猜你喜欢

转载自blog.csdn.net/qq_45696288/article/details/122200037