【发布时间】:2013-08-21 15:09:24
【问题描述】:
我的程序的一部分有问题。
在下面的代码中,我们的字母表中有 27 个字母。
目的是:对于外部for的每次重复,我们取text_generated的最后一个n字符,对于字母表中的每个字母,我们计算text_formatted中出现的次数n+1字符加上text_generated的最后一个n字符得到的字符;然后我们选择出现次数最多的字母并将其附加到text_generated。我得到的结果是这样的:
***aaaaaaaaaaaaaaaaaaa
为什么?
代码:
int index;
int[] occurrences = new int[27];
int count;
for(int a = 0; a < m; a++){ // m is the number of characters the user wants to add
for(int b = 0; b < 27; b++){
StringBuffer curr_word = new StringBuffer(text_generated.substring(text_generated.length()-n, text_generated.length()));
count = 0;
for(int c = 0; c <= text_formatted.length() -n-1;c++){
if(text_formatted.substring(c,c+n+1).equals(curr_word.append(array[b])))
count += 1;
}
occurrences[b] = count;
}
index = 0;
for(int d = 1; d < 27; d++){
if(occurrences[d] > occurrences[index])
index = d;
}
text_generated = text_generated.append(array[index]);
}
【问题讨论】:
标签: java string vector stringbuffer