输入一行字符,统计其中英文字母 空格 和其他字符

package p7;

import java.util.Scanner;

//输入一行字符,统计其中英文字母  空格  和其他字符
public class Test {
public static void main(String[] args) {
	System.out.println("输入一个字符串");
	Scanner sc=new Scanner(System.in);
	String str=sc.next();
	char c[]=str.toCharArray();   //字符串转成字符数组
	count.count(c);
}

}
class count{
	static int num; //分别是数字 字符 空格  其他
	static int chars;
	static int other;
	public  static void count(char c[]){
		for(int i=0;i<c.length;i++){  //遍历字符串
			if(c[i]>='0'&&c[i]<='9'){
				num++;
			}
			else if(c[i]>='a'&&c[i]<='z'){
				chars++;
			}
			else if(c[i]>='A'&&c[i]<='Z'){
				chars++;
			}
			else{
				
				other++;
			}			
		}
		System.out.println("数字有:"+num+"个");
		System.out.println("英文字母有:"+chars+"个");
		System.out.println("其他的字符有:"+other+"个");
	}
}

猜你喜欢

转载自blog.csdn.net/qq_37843372/article/details/83186762