【发布时间】:2018-03-11 18:36:51
【问题描述】:
我对 Java 比较陌生,但我正在努力掌握它!
所以在我的作业中,我被要求从用户(字符串)获取输入,并在输入字符串的每个单词的末尾附加一个单词。我可能错过了一些东西,因为我无法弄清楚这么简单的东西。
例如, 我们从用户那里得到输入
这就是我喜欢的方式
我希望它是:
这喵喵喵喵喵喵喵喵喵喵喵喵喵喵
我不能使用任何 if/for/while 语句/循环来解决这个问题,但是,我可以使用 Java String Class API
这是我目前的代码
public class Exercise2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter a string : ");
String userString = scan.nextLine();
String inputString = "meow";
//I'm aware that for the next like concatenating is just adding
//the word meow with the last word of the userString.
String newString = userString.concat(inputString);
scan.close();
}
}
问题:如何获得所需的输出?
【问题讨论】:
-
您必须将该字符串标记为单词,附加后缀,然后将它们重新组合在一起。你能做到每一步吗?
-
对不起,是的,基本上我如何获得所需的输出^^'
-
“单词”是如何定义的?在
Yes of course.中是course.(末尾带有点)是“单词”或course(不带点)。 -
@lexicore 如果您指的是标点符号,我不必担心。
-
@GeekyMeow 您应该定义什么是“单词”。用空格分隔的东西?
标签: java string append string-concatenation