错误地计算字符串中字符出现的百分比(java)



我的代码接受一个字符串,计算字母表中每个字符在字符串中出现的次数,然后显示数组的直方图,其中包含该字符在字符串中出现的次数的百分比信息。

我有问题,我的代码不显示正确的百分比,每个字符发生在字符串中,它是由几个百分点关闭。

例如,如果我输入"hello world",L应该占字符串的30%,对吗?但我的代码说它占了字符串的27%我不太确定这是代码问题还是计算问题

 public class LetterCounter {
    private static int[] alphabetArray; 
    private static char ch; 
    double stringLength; 

    /**
     * Creates an array to hold the total number of occurences for each character of the alphabet
     */
    public LetterCounter()
    {
        alphabetArray = new int[26]; 
    }
    /**
     * The method will go through the user inputted string, incrementing characters one by one 
     * when it comes across them in the string. 
     * @param input: the string that the user wants
     */
    public void countLetters(String input) {
        String userInput= input; 
        String s2 = userInput.toLowerCase();
        for ( int i = 0; i < s2.length(); i++ ) {
            char ch=  s2.charAt(i);
            if (ch >= 97 && ch <= 122){
                alphabetArray[ch-'a']++;
            }
        }
        stringLength = s2.length(); 
    }
    /**
     * Calculates how many characters there are in the inputted string. Multiple strings that are input by the user will be 
     * added to the total character count. 
     * @return: the sum of how many characters there are in each string
     */
    public int getTotalCount() {
        int sum=0; 
        for (int i = 0; i < alphabetArray.length; i++) {
            if(alphabetArray[i]>=0){
                sum = 0; 
                char ch = (char) (i+97); 
                for(int total : alphabetArray) {
                    sum += total;  
                }         
            }
        }
        return sum; 
    }
    /**
     * This method will reset the character count to zero for every character. 
     */
    public void reset() {
        for (int i : alphabetArray) {
            if(alphabetArray[i]>=0){
                alphabetArray[i]=0; 
                char ch = (char) (i+97); 
                System.out.println(ch +"  : "+alphabetArray[i]);   
            }    
        }
    }
    /**
     * The method prints out a histogram of the entire array, displaying the most commonly occuring letter as having 60 #s,
     * with the rest of the letter's #s to the 60 #s. The percentage occurence of each character in the string is also displayed beside the character. 
     * @return: the histogram of the array
     */
    public String toString() {
        int max = alphabetArray[0]; 
        int markCounter = 0;
        double percent = 0.0; 
        String s = ""; 
        for(int i =0; i<alphabetArray.length; i++) {
            //finds the largest number of occurences for any letter in the string
            if(alphabetArray[i] > max) {
                max = alphabetArray[i];
            }
        }
        for (int i = 0; i < alphabetArray.length; i++) {
            //percent = (alphabetArray[i] /stringLength) * 100; 
            percent = Math.round((alphabetArray[i] /stringLength ) * 100);
            markCounter = (alphabetArray[i] * 60) / max; 
            if(alphabetArray[i]>=0){
                char ch = (char) (i+97); 
                System.out.print(ch +" : ");   
            }  
            for (int hash =0; hash <markCounter; hash++) {
                System.out.print("#"); 
            }
            if(alphabetArray[i] >= 0){
                    System.out.print(" " + percent + "%"); //+ percent);
            }
            System.out.println(); 
        }
        return s; 
    }
}

您正在使用string.length()函数来计算字符串的长度。这个函数也会计算字符串长度中的空格。我猜你是在试图计算字符串中可用的总字符的百分比。

需要计算循环中的字符串长度,检查每个字符出现的频率。

public void countLetters(String input) {
    String userInput= input; 
    String s2 = userInput.toLowerCase();
    for ( int i = 0; i < s2.length(); i++ ) {
        char ch=  s2.charAt(i);
        if (ch >= 97 && ch <= 122){
            alphabetArray[ch-'a']++;
            stringLength++; 
        }
    }
}

这样您将获得字符串中字符的总数。

另外,您可能还想确定是否要包含字符而不是字母。特殊字符也可以是字符串的一部分。根据您的需求,字符串长度计数器将更新

String input = EditTextinput.getText().toString();输入=输入。

有时您可能只希望删除字符串开头或结尾的空格(而不是中间的空格)。如果是这种情况,你可以使用trim:

input = input.trim();

最新更新