next():
1.一直读取到有效字符,遇到空格会结束输入
2.在读取有效字符之前遇到的空格会自动删去
3,不会得到带空格的字符串
nextLine():
以enter为结束符,会输出enter之前的所有字符包括空格
代码
Scanner sc=new Scanner(System.in);
System.out.println("使用next方式接受:");
//判断用户有没有输入字符串
if(sc.hasNext()) {
String str=sc.next();
System.out.println(str);
}
//IO流的类如果不关会一直占用资源
sc.close();
Scanner sc=new Scanner(System.in);
System.out.println("使用nextLine方式接受:");
//判断用户有没有输入字符串
if(sc.hasNextLine()) {
String str=sc.nextLine();
System.out.println(str);
}
//IO流的类如果不关会一直占用资源
sc.close();