我已经尝试编程很长时间了,但它不起作用。我需要一个程序,它执行以下操作:
用户输入:我正在寻找一些食物。我喜欢食物。 系统打印:我是一些寻找食物的人。食物爱我。
反转单词,UpperWord 第一个单词,从下一个句子开始,保留“.”在它的地方。
package finaledition;
public class finaledition {
public static void main(String[] args) {
StringBuilder outputString= new StringBuilder();
String satz;
String wort;
System.out.print("Bitte geben Sie einen String ein: ");
String text = Input.readString();
while (text.indexOf('.') > 0) {
satz = text.substring(0, text.indexOf('.'));
text = text.substring(text.indexOf('.') + 1);
while (satz.lastIndexOf(' ') > 0) {
wort = satz.substring(satz.lastIndexOf(' ') + 1);
outputString.append(wort);
satz = satz.substring(0, satz.lastIndexOf(' '));
}
System.out.print(outputString);
}
}
}
实际结果是:
foodsomeforlookingfoodsomeforlookingfoodlove
最佳答案
首先,您需要将洞文本拆分为句子:
String[] sentences = text.split(". ");
然后你需要用以下单词创建一个数组:
String[] words = sentences[anIndex].split(" ");
然后,反转单词数组:
String[] reverseSentence = new String[words.length];
for(int a=0; a<words.length; a++){
reverseSentence[a] = words[words.length - a -1];
}
然后将所有的倒装句加在一起并添加一个“.”。
关于java - 反转所有单词,设置 "."并对接下来的句子执行相同的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47637713/