Bootstrap

输入一行字符串分别统计出字母。数字。空格和其他字符 *利用while循环语句条件输入的字符为"\n"

package zy;

import java.util.Scanner;
import java.util.regex.Pattern;

/**
* 输入一行字符串分别统计出字母。数字。空格和其他字符
* 利用while循环语句条件输入的字符为”\n”
* @author Administrator
*
*/
public class Test3 {
public static void main(String[] args) {
System.out.println(“请输入一串字符:”);
Pattern m = Pattern.compile(“\d”);
Pattern z = Pattern.compile(“[a-zA-Z]”);
Pattern k = Pattern.compile(“\s”);
Scanner sc = new Scanner(System.in);
String msg=sc.next();
int sz=0,zm=0,kg=0,o=0;
char[] cs = msg.toCharArray();
for (char c : cs) {
String s = String.valueOf(c);
//判断s是否满足正则表达式
if (m.matcher(s).matches()) {
sz++;
}else if (z.matcher(s).matches()) {
zm++;
}else if (k.matcher(s).matches()) {
kg++;
}else {
o++;
}
}
System.out.println(“字母有”+zm+”个”);
System.out.println(“数字有”+sz+”个”);
System.out.println(“空格有”+kg+”个”);
System.out.println(“其他字符有”+o+”个”);
sc.close();
}
}

;