您的位置:首页 > 编程语言 > Java开发

Java 统计字符串里英文字母、空格、数字和其它字符的个数

2016-01-09 16:34 603 查看
import java.util.Scanner;

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

public class Six {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("输入一行字符串:");
String str = input.nextLine();

account(str);
}

public static void account(String str) {
int acc1 = 0;   //统计英文字母个数
int acc2 = 0; //统计空格个数
int acc3 = 0; //统计数字个数
int acc4 = 0; //统计其他字符个数

for(int i=0;i<str.length();i++) {
if(Character.isLetter(str.charAt(i)))
acc1 += 1;
else if(Character.isDigit(str.charAt(i)))
acc3 += 1;
else if(Character.isSpaceChar(str.charAt(i)))
acc2 += 1;
else
acc4 += 1;
}
System.out.println("英文字母有" + acc1 + "个");
System.out.println("数字有" + acc3 + "个");
System.out.println("空格母有" + acc2 + "个");
System.out.println("其他字符有" + acc4 + "个");
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: