java计算字符串中的大写字母、小写字母和非字母的个数

效果如图:

代码如下:

import java.util.Scanner;

public class Test_String1 {
	public static void main(String[] args) {
		System.out.println("请输入字符串:");
		//从控制台输入字符串
		Scanner sc=new Scanner(System.in);
		String s=sc.next();
		
		int upperCount=0,lowCount=0,otherCount=0;
		for(int i=0;i<s.length();i++) {
			char c=s.charAt(i);
			if(c>='A' && c<='Z') {
				upperCount++;
			} else if(c>='a' && c<='z') {
				lowCount++;
			}else {
				otherCount++;
			}		
		}
		System.out.print("大写字母有"+upperCount+"个,");
		System.out.print("小写字母有"+lowCount+"个,");
		System.out.print("非字母有"+otherCount+"个。");	
	}
}

猜你喜欢

转载自blog.csdn.net/maidu_xbd/article/details/85811798