Java小细节之next()与nextLine()的区别

Scanner in = new Scanner(System.in);
String s;
s = in.next();
System.out.println(s);
首先,next和nextLine都可以读入,next()的规则是读到第一个空格就停止,这个空格是广义上的空格,包括Tab或者换行。
以上程序,我们输入hello world,输出只会得到 hello。
Scanner in = new Scanner(System.in);
String s;
s = in.nextLine();
System.out.println(s);
nextLine()会读入一整行,当我们输入hello world的时候,我们就会得到hello world。

posted @ 2024-01-16 13:14  绿皮玻璃  阅读(98)  评论(0)    收藏  举报