public class Testsuanfa(){
public static int [] count(String str){
int [] count = new int[26]; //存放26个英文字母 count[0] = A
for(int i = 0; i < str.length(); i++) { //遍历
char c = str.charAt(i); //取出每一个字符
if(c >= ‘A’ && c <= ‘Z’) { //判断在这个范围内(判断是否为大写字母)
count[c - ‘A’]++; //计算字母出现的次数 就存在这个数组中了
}
}
return count;
}
public static void main(String []args){
//目标 统计出大写字母的出现次数
String str = “asdfEEfAEDGasd123A你好”;
int [] count = count(str);
char c = ‘A’;
for(int i = 0; i < 26; i++) {
System.out.println(c++ + “:” + count[i]);
}
}
}
Java(入门)从字符串中获取大写字母出现的次数
最新推荐文章于 2022-09-20 14:50:51 发布