【发布时间】:2020-02-04 18:21:11
【问题描述】:
我想从用户输入中打印几个词, 例如那句话:“我们爱妈妈和爸爸” 该程序将打印“妈妈爸爸” 我不知道如何打印这些词。只是字符 m 和 d。 非常感谢 ! 这是我的代码:
Scanner s = new Scanner(System.in);
System.out.println("Please enter a Sentence");
String input = s.nextLine();
String output = "";
for (int i = 0, j = 0; i < input.length(); i++) {
if (input.charAt(i) == ' ') {
j = i + 1;
}
if (j < i && input.charAt(j) == input.charAt(i)) {
output=output+(input.charAt(i);
}
}
System.out.println(output);
}
}
【问题讨论】:
-
你刚才不是问那个问题吗:stackoverflow.com/questions/60059612/…
-
不,因为它不是回文。就一个字
-
@TalOuzan 这个问题包括几个步骤:1) 将一个句子分解成单个单词,2) 测试每个单词看它是否是回文,3) 如果它是一个回文,则打印出整个单词回文。现在看起来你并没有真正做任何这些事情。第一步是将句子分解成单词。所以从那开始,看看你能不能完成它。然后从那里继续前进。
标签: java string palindrome