Scanner
【作用】:用于接收用户的输入。
Scanner scanner = new Scanner(System.in);
涉及的构造方法:
通过 scanner 调用下列方法时都会造成堵塞,等待用户在命令行输入数据回车确认:
next.Byte( ),nextDouble( ),nextFloat( ),nextInt( ),nextLine( ),nextLong( ),nextShort( )
循环扫描
next()和 nextLine()的区别
- next 在遇到有效字符后,才会将空格、Tab 键、Enter 键视为结束符,所以 next 不能得到带空格的字符串,返回的时候会将换行(\n)一同返回
- nextLine 以 ENTER 为结束符,也就是返回的是输入回车之前的字符,返回的时候会将换行(\n)一同返回
next()和 nextLine()方法的连用
hasNext()和 hasNextLine()的区别
- hasNext 检测还有没有下一个输入
- hasNextLine 检测下一行有没有输入