0
因此,如題目所示,我有一個作業問題,我遇到了麻煩。只使用基本的字符串方法(length,charAt,substring,equals,equalsIgnoreCase)和循環,我必須替換用戶輸入字符串中所有字符的實例,並用新字符替換用戶想要的新字符舊。我不知道如何在不使用indexOf的情況下找到並存儲字符串字符的所有實例。這是我寫的一些基本代碼,我們將不勝感激。我不知道如何只用這些基本的字符串方法替換一個字符的所有實例。僅使用循環替換字符串中的所有字符實例
String letter;
System.out.println("Enter the character you want to replace");
input = keyboard.nextLine();
int characterAt = 0;
for (characterAt = 0; characterAt < savedInput.length(); characterAt++)
{
letter = "" + savedInput.charAt(characterAt);
if(input.equals(letter))
{
System.out.println("Enter the new character");
input = keyboard.nextLine();
System.out.println("The new string is: ");
}
}