我需要帮助来完成此代码。我试图通过一个单词中的每个字母并使用replacementLetter更改letterToReplace。问题是我不能使用.replace
请帮忙
public String replaceLetter(String word, char letterToReplace, char replacingLetter)
{
String ans = "";
for(int i = 0; i < word.length(); i++)
{
char current = word.charAt(i);
if(current == letterToReplace)
{
letterToReplace = replacingLetter;
}
ans += Character.toString(word.charAt(i));
}
return ans;
}
答案 0 :(得分:1)
你几乎就在那里。您只需更新并打印current
:
if(current == letterToReplace)
{
current = replacingLetter;
}
ans += Character.toString(current);