[英]My program needs to count the number of occurences of letters in a word, but it seems to count only the occurence of letter 'a'
I need to accept a String and check how many letters of the alphabet appeared on it. 我需要接受一个字符串,并检查它上出现了多少个字母。 However, it seems to only count how many times 'a' appears and concatenate that count to all the letters.
但是,似乎只计算出现一次“ a”并将其连接到所有字母的次数。 I can't find where the error is.
我找不到错误在哪里。 Any help will be appreciated.
任何帮助将不胜感激。 Thanks in advance!
提前致谢! Here's my code and output.
这是我的代码和输出。
package javaPackage;
public class Counter {
public static void main(String[] args) {
String letter="abcdefghijklmnopqrstuvwxyz";
String word="banana";
char letterArray[]=letter.toCharArray();
int length = word.length();
int count = 0;
int index = 0;
for (int i=0;i<26;i++) {
while (index < length) {
if (word.charAt(index) == letterArray[i]) {
count++;
}
index++;
}
System.out.println("Letter " + letterArray[i]+ " appeared "
+ count+ " times");
}
}
}
Output: 输出:
Letter a appeared 3 times
Letter b appeared 3 times
Letter c appeared 3 times
Letter d appeared 3 times
Letter e appeared 3 times
Letter f appeared 3 times
Letter g appeared 3 times
Letter h appeared 3 times
Letter i appeared 3 times
Letter j appeared 3 times
Letter k appeared 3 times
Letter l appeared 3 times
Letter m appeared 3 times
Letter n appeared 3 times
Letter o appeared 3 times
Letter p appeared 3 times
Letter q appeared 3 times
Letter r appeared 3 times
Letter s appeared 3 times
Letter t appeared 3 times
Letter u appeared 3 times
Letter v appeared 3 times
Letter w appeared 3 times
Letter x appeared 3 times
Letter y appeared 3 times
Letter z appeared 3 times
You never reset your counter
and index
variables : 您永远不会重置
counter
和index
变量:
for (int i=0;i<26;i++) {
index = 0; // added
counter = 0; // added
while (index < length) {
if (word.charAt(index) == letterArray[i]) {
count++;
}
index++;
}
}
That said, this implementation is very inefficient. 也就是说,这种实现方式效率很低。 You can count all occurrences in a single for loop.
您可以在一个for循环中计算所有出现的次数。
countArray[0]
will count the occurrences of 'a', countArray[1]
will count the 'b's, and so on. countArray[0]
将计算'a'的出现, countArray[1]
将计算'b'的出现,依此类推。
String word="banana";
int countArray[]=new int[26];
for (int i = 0; i < word.length(); i++) {
countArray[word.charAt(i) - 'a']++;
}
for (int i = 0; i < countArray; i++) {
System.out.println("Letter " + (char)('a' + i) + " appeared " + countArray[i] + " times");
}
你是不是重新index
和count
。所以它显示了获得的结果a
而已。
You have to reset index to 0 once you left the inner loop. 离开内循环后,必须将索引重置为0。 Same with count.
与计数相同。 Otherwise he only runs the inner loop once for the letter A and then never again, because the condition in the while isn't fulfilled.
否则,他只对字母A运行一次内部循环,然后再也不运行一次,因为不满足while条件。
Edit: efficient solution 编辑:高效的解决方案
int[] letterOccurences = new int[26];
for (int i = 0; i < letterOccurences.length; i++) {
letterOccurences[i] = 0;
}
for (int i = 0; i < word.length(); i++) {
letterOccurences[letter.charAt(i) % 97]++;
// 'a' % 97 will return 0, 'b' % 97 will return 1 and so on.
//Thus, in 0 the count of 'a's, in 1 the count of 'b's and so on will be tracked.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.