我正在编写一个从句子中删除一个字符的java程序。我的代码是;
public class Ex1Program {
public void start() {
String sentence = getSentenceFromUser();
int randomPosition = getRandomPosition(sentence);
printCharacterToBeRemoved(sentence, randomPosition);
String changedSentence = removeCharacter(sentence, randomPosition);
printNewSentence(changedSentence);
}
private String getSentenceFromUser() {
System.out.print("Enter a sentence :");
String sentence = Keyboard.readInput();
return sentence;
}
int sentenceLength = sentence.length();
int randomPosition = (int)(Math.random() * sentenceLength) + 0;
return randomPosition;
}
private void printCharacterToBeRemoved(String sentence, int randomPosition) {
System.out.print("Removing " + sentence.charAt(randomPosition) + " from position" + randomPosition);
}
private String removeCharacter(String sentence, int randomPosition) {
String changedSentence = sentence.deleteCharAt(randomPosition);
return changedSentence;
}
private void printNewSentence(String changedSentence) {
System.out.print("New sentence is " + changedSentence);
}
}
我在编译时收到一条错误消息;
错误:找不到符号符号:方法 deleteCharAt(int) 位置:java.lang.String 类型的变量语句
我花了几个小时试图解决这个问题,所以非常感谢您的帮助。