Scanner类中next()方法和hasNext()方法的区别
从键盘输入值
Scanner scanner = new Scanner(System.in); String next = scanner.next(); System.out.println(next);
next()方法只有扫描到有效字符后才会结束输入(用户没有输入的时候不会结束);next()方法会自动去掉空白(例如回车、空格等),也不能得到带有空格的字符串;输入hello world,结果只会出来hello,空格以及后面的world会去除
nextLine()方法可以直接使用回车结束输入。nextLine()方法可以得到空白和带有空格的字符串。
在IDEA中会出现test测试类中没办法使用scanner的情况
解决方法:点击help->edit custom vm options
在末尾添加:
-Deditable.java.test.console=true