Scanner in = new Scanner(System.in);
String s = in.nextLine();
String[] strings = s.split("\\n");
for (int i = 0; i < strings.length; i++) {
System.out.println(strings[i]);
}
该字符串未被 \n
分割。我不明白为什么。示例输入为 Apples\nOranges\nMangoes
。
最佳答案
The string isn't being split by
\n
.
该字符串不包含 \n
。请参阅 Scanner.nextLine()
的 Javadoc。
如果你想读取并打印所有行,你就必须读取所有行。在一个循环中。为此,您不需要 String.split()
。
关于java - String.split ("\n")不适用于扫描仪输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46879532/