计数字符数组中的字符


听起来像计算机科学不是你的一杯茶。 我强烈建议你重构这段代码,并试图找出它为什么会这样做。

public void print(String ht) { // print array

    String st = ht.toLowerCase().replaceAll("s", "");
    sent = st.toCharArray();

    int[] alphabet = new int[26];
    //set all values to 0
    for(int i = 0 ; i < alphabet.length ; i++){
        alphabet[i] = 0;
    }
    //Loop through all characters and increment corresponding value
    for(int i = 0 ; i < sent.length ; i++){
        alphabet[sent[i] - 97]++;
    }
    //Print character + asterisk for each time the character is used
    for(int i = 0 ; i < alphabet.length ; i++){
        System.out.print((char)(i + 97) + ": ");
        for(int nChars = 0 ; nChars < alphabet[i] ; nChars++){
            System.out.print("*");
        }
        System.out.println();
    }

}
链接地址: http://www.djcxy.com/p/20311.html

上一篇: Counting characters in a Character array

下一篇: What's the @ in front of a string in C#?