gpt4 book ai didi

java - 如何返回对字符串的更改

转载 作者:行者123 更新时间:2023-12-01 11:59:08 26 4
gpt4 key购买 nike

如何调整 Changer 类中的更改方法,以便它将执行所有添加的更改并返回包含更改的字符串?我在尝试在 for 循环中实现此操作时遇到问题。输出的字母上不应有任何重音符号。

public class Change {
private char fromCharacter;
private char toCharacter;

public Change(char fromCharacter, char toCharacter) {
this.fromCharacter = fromCharacter;
this.toCharacter = toCharacter;
}

public String change(String characterString) {
String newWord = characterString.replace(fromCharacter, toCharacter);
return newWord;
}
}

import java.util.ArrayList;

public class Changer {
private ArrayList<Change> changes;

public Changer() {
this.changes = new ArrayList<Change>();
}

public void addChange(Change change) {
this.changes.add(change);
}

public String change(String characterString) {
String word = "";

for (Change change : changes) {
word = change.change(characterString);
}

return word;
}
}

public class Main {
public static void main(String[] args) {
Changer scandiesAway = new Changer();
scandiesAway.addChange(new Change('ä', 'a'));
scandiesAway.addChange(new Change('ö', 'o'));
System.out.println(scandiesAway.change("ääliö älä lyö, ööliä läikkyy"));

}
}

最佳答案

按如下方式更改您的方法;

更改

public String change(String characterString)  {       
return characterString.replace(fromCharacter, toCharacter);
}

转换器

public String change(String characterString) {

for (Change change : changes) {
characterString = change.change(characterString);
}

return characterString;
}

关于java - 如何返回对字符串的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28117514/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com