我如何 return 更改为字符串
How can I return changes to a String
如何在我的 Changer class 中调整我的更改方法,以便它执行所有添加的更改和 return 包含更改的字符串?我在 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"));
}
}
改变你的方法如下;
Class Change
public String change(String characterString) {
return characterString.replace(fromCharacter, toCharacter);
}
Class Changer
public String change(String characterString) {
for (Change change : changes) {
characterString = change.change(characterString);
}
return characterString;
}
如何在我的 Changer class 中调整我的更改方法,以便它执行所有添加的更改和 return 包含更改的字符串?我在 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"));
}
}
改变你的方法如下;
Class Change
public String change(String characterString) {
return characterString.replace(fromCharacter, toCharacter);
}
Class Changer
public String change(String characterString) {
for (Change change : changes) {
characterString = change.change(characterString);
}
return characterString;
}