java基础(读取键盘输入Scanner)
输入
例:
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
double nums = scanner.nextDouble();
String name = scanner.nextLine();
String names = scanner.nextLine();
System.out.println("___________");
System.out.println(num);
System.out.println(nums);
System.out.println(name);
System.out.println(names)
输入输出:
1
1.111
张三
___________
1
1.111
张三
整形:nexInt();
浮点型:nextDouble()
字符输入:next(),nextLine()
next()与next Line区别
int i = scanner.nextInt();
String name = scanner.next();
int j = scanner.nextInt();
String names = scanner.nextLine();
System.out.println("###########");
System.out.println(i);
System.out.println(name);
System.out.println(j);
System.out.println(names);
输入输出:
1
张三
2
###########
1
张三
2
四条输入语句,结果只能输入三次
因为回车键会直接跳过nextLine的输入;
输入 a b c;
next 会输出 a
nextLine 输出 a b c;