編寫一個程序,統計輸入字符串中每一個小寫英文字母出現的次數
阿新 • • 發佈:2019-03-02
char public script out 小寫 字符數組 esc page ted
import java.util.Scanner; /** * @author:(LiberHome) * @date:Created in 2019/3/1 22:18 * @description: * @version:$ */ /*編寫一個程序,統計輸入字符串中每一個小寫英文字母出現的次數*/ public class page0901 { public static void main(String[] args) { /*首先,輸入一段字符串作為字符數組*/ System.out.println("請輸入一段字符串"); Scanner scanner= new Scanner(System.in); String s = scanner.nextLine(); char[] chars = s.toCharArray(); countLeters(chars); } private static void countLeters(char[] arr) { /*然後,初始化一個長度為26的整型數組,初始值全為0*/ int[] temp = new int[26]; for (int i = 0; i < temp.length; i++) { temp[i]=0; } /*遍歷字符數組,將其值對應的ascll值作為整型數組下標,++*/ for (int i = 0; i < arr.length; i++) { if (arr[i]>=‘a‘&&arr[i]<=‘z‘){ temp[arr[i]-‘a‘]++; } } /*最後打印輸出每一個小寫英文字母及其出現的次數*/ for (int i = 0; i <26 ; i++) { System.out.println((char)(i+‘a‘)+"出現了 : "+temp[i]+" 次"); } } }
編寫一個程序,統計輸入字符串中每一個小寫英文字母出現的次數