我只是好奇:扫描仪为何如此操作?
假设我设置了扫描仪。
Scanner sc = new Scanner (System.in)
int number = sc.nextInt();
String name = sc.nextLine();
System.out.println (number);
System.out.println (name);
会发生什么事情,我甚至不会输入名称,所有打印的数字都是数字。相反,如果我这样做:
Scanner sc = new Scanner (System.in)
int number = sc.nextInt();
String name = sc.next();
System.out.println (number);
System.out.println (name);
然后,一切工作顺利,但不能在字符串中使用空格。
为什么被扫描的字符串在被一个数字后面显得很滑稽。如果我只是使用2个字符串,它不会做它看起来。
我知道解决的办法是在两者之间放一条空白线,但我只想知道为什么发生这种情况。
难道你不把“空间”与“新行”字符混淆? – zubergu 2015-04-02 06:56:06
你的困惑可能是'next()'和'nextLine()'? – Prashant 2015-04-02 06:57:48
不,我不是。如果我想用第二个例子写出字符串“This blows”,那么所有打印的内容都是This。 – 2015-04-02 06:59:07