当一行输入字符串和数字时,为了能取到值
12 abc
Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); String[] s3 = s.split(" "); int n=Integer.parseInt(s3[0]); System.out.println(n); String s4=s3[1]; System.out.println(s4);
如果一行输入数字一行输入字符串的话
比如
12
aaaaaa
Scanner s = new Scanner(System.in); int n=s.nextInt(); String s1 = s.next(); System.out.println(s1);
或者
Scanner s = new Scanner(System.in); int n=s.nextInt(); s.nextLine(); System.out.println(n); String s1 = s.nextLine(); System.out.println(s1.charAt(0));
但这样写是不对的
Scanner s = new Scanner(System.in); int n=s.nextInt(); //s.nextLine(); System.out.println(n); String s1 = s.nextLine(); System.out.println(s1.charAt(0));
因为nextInt会把空格和回车保存下来