所以我想从字母表中删除modifiedKeyword(PLAIN_ALPHA用作常量)
private String removeKeyword(String modifiedKeyword){
int wordLength = modifiedKeyword.length();
int letterPosition;
int alphabetPosition;
String modifiedAlphabet = "";
final String PLAIN_ALPHA = "bcdefghijklmnopqrstuvwxyz";
for (alphabetPosition=0; alphabetPosition<=26; letterPosition++){
boolean shouldBeDeleted = false;
char alphabetLetter = PLAIN_ALPHA.charAt(alphabetPosition);
for (letterPosition=0; letterPosition>wordLength; letterPosition++){
char keywordLetter = modifiedKeyword.charAt(letterPosition);
if (alphabetLetter == keywordLetter) { shouldBeDeleted = true;}
modifiedAlphabet += alphabetLetter;
}
}
return modifiedAlphabet;
}
由于某种原因,当我运行它时,它不会执行但只会继续加载(鼠标光标显示加载动画)永远。你们可以发现我一定错过的任何错误吗?感谢。
答案 0 :(得分:2)
for (alphabetPosition=0; alphabetPosition<=26; letterPosition++){
你永远不会增加计数器变量alphabetPosition
,所以你的代码在无限循环中运行。
修改强>
使用String#replaceAll(,String, String)
还可以更轻松地完成此操作private String removeKeyword(String modifiedKeyword){
final String PLAIN_ALPHA = "bcdefghijklmnopqrstuvwxyz"; //try to extract this as a constant for cleaner code
String result = PLAIN_ALPHA.replaceAll(modifiedKeyword, "");
return result;
}
答案 1 :(得分:1)
for (alphabetPosition=0; alphabetPosition<=26; letterPosition++){
您正在递增另一个变量而不是您初始化的变量。 (事实上,你永远不会增加alphabetPosition)。
for (letterPosition=0; letterPosition>wordLength; letterPosition++){
letterPosition可能不比你的字长更好,对吗?
答案 2 :(得分:0)
当这些事情发生时,你应该总是仔细检查你的循环,它很可能是无限循环。在您的情况下,alphabetPosition
始终为0,永远不会达到27:
for (alphabetPosition=0; alphabetPosition<=26; letterPosition++){