next()方法會(huì)將空格識(shí)別為輸入結(jié)束的標(biāo)識(shí)。
(推薦教程:java入門(mén)教程)
代碼示例:
String str1 = sc.next(); System.out.println(str1);
輸入 abcd efg 輸出 abcd
nextLine()方法會(huì)將換行識(shí)別為輸入結(jié)束的標(biāo)識(shí)。
(視頻教程推薦:java視頻教程)
代碼示例:
String str1 = sc.nextLine(); System.out.println(str1)
總結(jié):
需要空格截?cái)噍斎氲那闆r使用next(),比如輸入1(空格)2,可以得到a=1,b=2;
需要含空格的情況使用nextLine(),如輸入1(空格)2,可以得到a = 1(空格)2。