介绍一下next方法和nextLine方法的区别
- 第一种情况,nextLine遇到换行符结束输入,next遇到空白、制表、回车符结束输入,输入数据没有空格的情况。
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("请输入");
String s1 = in.nextLine();
System.out.println(s1);
System.out.println("--------");
System.out.println("请输入");
String s2 = in.next();
System.out.println(s2);
}
控制台结果:
请输入
好好学习
好好学习
--------
请输入
天天向上
天天向上
Process finished with exit code 0
- 第二种情况,如果输入数据中有空格,则两种读取方式不一样了,nextLine仍然读取一行,而next只能读取到空格位置。
public static void main(String[] args) {
Scanner in =