Scanner中next()和nextLine()方法的区别
2018-03-21 21:13
302 查看
程序一:
程序二:
程序三:
程序四:
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner scanner=new Scanner(System.in); String s1=scanner.nextLine(); String s2=scanner.next(); System.out.println(s1+"..."+s2); scanner.close(); } } 控制台1: home work home...work 控制台2: 我 啦啦 你 嗯嗯 我 啦啦...你 结论: nextLine()包含空格 可以得到带空格的字符串。 next()没有空格 不能得到带空格的字符串。 读到空白符就结束,一定要读取到有效字符后才可以结束输入,对输入有效字符之前遇到的空格键、Tab键或Enter键等结束符,next()方法会自动将其去掉,只有在输入有效字符之后,next()方法才将其后输入的空格键、Tab键或Enter键等视为分隔符或结束符。
程序二:
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner scanner=new Scanner(System.in); String s1=scanner.next(); String s2=scanner.next(); System.out.println(s1+"..."+s2); scanner.close(); } } 控制台: home work home...work
程序三:
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner scanner=new Scanner(System.in); String s1=scanner.nextLine(); String s2=scanner.nextLine(); System.out.println(s1+"..."+s2); scanner.close(); } } 控制台: home work home...work
程序四:
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner scanner=new Scanner(System.in); String s1=scanner.next(); String s2=scanner.nextLine(); System.out.println(s1+"..."+s2); scanner.close(); } } 控制台: home home...
相关文章推荐
- Scanner中next()方法和nextLine()方法的区别
- java中Scanner类nextLine()和next()的区别和使用方法
- java中Scanner类nextLine()和next()的区别和使用方法
- Java的Scanner类中next()方法与nextLine()方法的区别
- Java中next()方法与nextLine()的区别
- Java中 next() 方法和 nextLine() 方法的区别?
- java中Scanner类nextLine()和next()的区别和使用方法
- 使用next()和nextLine()方法接收从键盘输入字符串型数据区别
- Scanner中next()和nextLine()方法的区别
- Scanner中nextLine()方法和next()方法的区别
- JAVA Scanner类里next方法和nextLine方法的区别
- java中next()和nextLine()方法的区别
- next方法和nextLine方法的区别?
- java Scanner中的 next() 方法和 nextLine() 方法的区别
- JAVA scanner类中next()方法与nextline()方法的区别
- java中Scanner类nextLine()和next()的区别和使用方法
- nextLine()和next()的区别和使用方法
- java中Scanner类nextLine()和next()的区别和使用方法
- 总结Java中的next()和nextLine()方法的区别
- 关于java中的Scanner类中的nextLine方法与next方法的区别