package Test_01;
import java.util.Scanner;
public class 统计字符串 {
/**
* @param args
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.err.println("请输入一个字符串");
String s = sc.nextLine();
int x = 0;
int y = 0;
int z = 0 ;
int w = 0;
char[] ch = s.toCharArray();
for (int i = 0; i < ch.length; i++) {
if('0'<=ch[i]&&ch[i]<='9'){
x++;
}else if (('a'<=ch[i]&&ch[i]<='z')||('A'<=ch[i]&&ch[i]<='Z')) {
y++;
}else if (ch[i]==' ') {
z++;
}else {
w++;
}
}
System.out.println("字母出现次数为"+y+"数字出现次数为"+x+"空格出现次数为"+z+"其他字符出现次数为"+w);
}
}
|
|