I have a class called LetterCounter. This class processes strings by turning the letters of a string into chars and adding them to an array called counts[] with 26 positions, with 0 corresponding to 'a' and 25 corresponding to 'z'. This is what the process() method looks like.
我有一门课叫“信得过”。这个类通过将字符串的字母转换成字符来处理字符串,并将它们添加到一个名为counts[]的数组中,该数组有26个位置,0对应于“a”,25对应于“z”。process()方法是这样的。
public class LetterCounter
{
private static final int[] counts = new int[26];
/**
* Method converts letters in a String into index positions in an array.
* 0 corresponds to a, 1 to b, 2 to c, etc.
*
* @param someString A String of letters
*/
public void process(String someString)
{
for (int i = 0; i < someString.length(); i++)
{
char myChar = Character.toLowerCase(someString.charAt(i));
int index = (myChar - 'a');
if ((index >= 0) && (index <= 25))
{
counts[index]++;
}
}
}
I have a method in this class that I call getMostCommon() which iterates over the array and determines which position has the most stored items. It looks like this:
我在这个类中有一个名为getMostCommon()的方法,它遍历数组并确定哪个位置的存储项最多。它看起来像这样:
/**
* Method finds the letter which appears most often.
*/
public char getMostCommon()
{
int max = 0;
for(int i = 0; i < counts.length; i++)
{
if(counts[i] > max)
{
max = i;
}
}
char c = Character.toLowerCase((char)(max + 'a'));
return c;
}
Here is the test class. I expect 't' to be the most common letter, but the method returns 'o' as the most common letter.
这是测试类。我希望't'是最常见的字母,但方法返回'o'作为最常见的字母。
public class CounterDemo1
{
public static void main(String[] args)
{
//Constructs new LetterCounter called cc
LetterCounter cc = new LetterCounter();
//The letters in these strings will be processed into array index positions.
cc.process("Bojack hates the troops");
cc.process("Peanut butter is one word");
cc.process("Use a pretty font");
//Demonstrates printHistogram()
cc.printHistogram();
//Demonstrates getCount()
System.out.println("There are " + cc.getCount('b') + " b's.");
//Demonstrates getToalLetters()
System.out.println("There are " + cc.getTotalLetters() + " total letters.");
//Demonstrates getMostCommon()
System.out.println("The most common letter is: " + cc.getMostCommon());
}
}
}
1 个解决方案
#1
5
You need to store the value of the maximun character frequency so far: max and the current character index: i.
到目前为止,您需要存储最大字符频率的值:max和当前字符索引:i。
You are comparing max with the frequency of the character i:if(counts[i] > max)
and then assigning the value of i to max: max = i;
你在比较max与字符i:if(count [i] > max)的频率,然后将i的值赋给max: max = i;
You probably want to do this:
你可能想这样做:
public char getMostCommon()
{
int max = 0;
int current_index = 0; // current character index
for(int i = 0; i < counts.length; i++)
{
if(counts[i] > max)
{
max = counts[i]; // Getting the value of the frequency
current_index = i; // And the value of the character index
}
}
// Finally parsing the index
char c = Character.toLowerCase((char)(current_index + 'a'));
return c;
}
#1
5
You need to store the value of the maximun character frequency so far: max and the current character index: i.
到目前为止,您需要存储最大字符频率的值:max和当前字符索引:i。
You are comparing max with the frequency of the character i:if(counts[i] > max)
and then assigning the value of i to max: max = i;
你在比较max与字符i:if(count [i] > max)的频率,然后将i的值赋给max: max = i;
You probably want to do this:
你可能想这样做:
public char getMostCommon()
{
int max = 0;
int current_index = 0; // current character index
for(int i = 0; i < counts.length; i++)
{
if(counts[i] > max)
{
max = counts[i]; // Getting the value of the frequency
current_index = i; // And the value of the character index
}
}
// Finally parsing the index
char c = Character.toLowerCase((char)(current_index + 'a'));
return c;
}