【发布时间】:2020-03-17 04:42:45
【问题描述】:
Scanner sc = new Scanner(System.in);
String str = sc.next();
System.out.println(str);
Provided input : this is a good school
Obtained Output : this
为什么没有打印完整的字符串?
【问题讨论】:
Scanner sc = new Scanner(System.in);
String str = sc.next();
System.out.println(str);
Provided input : this is a good school
Obtained Output : this
为什么没有打印完整的字符串?
【问题讨论】:
【讨论】:
来自Scanner.next()的文档:
java.util.Scanner.next() 方法查找并返回下一个 来自此扫描仪的完整令牌。前面有一个完整的令牌,并且 后跟与分隔符模式匹配的输入。
https://www.tutorialspoint.com/java/util/scanner_next.htm
如果您想阅读整行,最好使用以下代码:
BufferedReader buffer=new BufferedReader(new InputStreamReader(System.in));
String line=buffer.readLine();
【讨论】: