[上]JAVA学习系列模块四第一章79.Scanner键盘录入_next&nextLine区别
视频
笔记
变量名.next():录入字符串 -> 遇到空格和回车就结束录入了
变量名.nextLine():录入字符串 -> 遇到回车就结束录入
public class Demo03Scanner { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String data1 = sc.next(); String data2 = sc.nextLine(); System.out.println(data1); System.out.println(data2); } }
Exception in thread "main" java.util.InputMismatchException -> 输入类型不匹配异常 at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at com.atguigu.a_scanner.Demo04Scanner.main(Demo04Scanner.java:8) 原因:录入的数据和要求的数据类型不一致