import java.util.Scanner; public class Demo2 { public static void main(String[] args) { //首先输入一串字符,并将字符转化为小写 Scanner scanner = new Scanner(System.in); System.out.println("请输入一串英文字母"); char[] str = scanner.next().toLowerCase().toCharArray(); //利用for()循环将字符从a-z 便利一遍,再依次找出出现的次数 System.out.println("每个字符出现的次数是:"); for (char j = ‘a‘; j < ‘z‘; j++) { int count = 0; for (int i = 0; i < str.length; i++) { if (j == str[i]) { count++; } } //假如该字符存在 if (count != 0) { System.out.println("字母" + j + "出现:" + count + "次"); } } }}
原文地址:https://www.cnblogs.com/anonymityning/p/Mr_wei154655.html
时间: 2024-11-05 14:45:27