public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine().replaceAll("\s*","");
printLetterNum(str);
}
public static void printLetterNum(String str) {
//按照字符在字符串中的位置排序
Map<Character, Integer> maps = new LinkedHashMap<>();
//按照自然顺序排序
//SortedMap<Character, Integer> maps = new TreeMap<>();
for (int i = 0; i < str.length(); i++) {
if (!Character.isDigit(str.charAt(i))) {
if (maps.containsKey(str.charAt(i))) {
maps.put(str.charAt(i), maps.get(str.charAt(i)) + 1);
} else {
maps.put(str.charAt(i), 1);
}
}
}
for (Map.Entry<Character, Integer> entry : maps.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
}
}
最后
以上就是震动白猫最近收集整理的关于华为机试题---控制台输入统计并输出每个字符在字符串中出现的次数,数字和空格除外,并按原始位置排序的全部内容,更多相关华为机试题---控制台输入统计并输出每个字符在字符串中出现内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复