java 输入scanner
Scanner scanner = new Scanner(System.in);
System.out.println("请输入");
String str=scanner.nextLine();//还可以是scanner.next()
System.out.println(str);
//一定要关掉
scanner.close();
scanner.next()读不了空格
scanner.nextLine()能
关于scanner扩展
可以有while循环让其即使按回车键也可以持续性输入,
Scanner sc = new Scanner(System.in);
System.out.println("请输入");
//当输入的数据为Int时会一直处于输入状态
while(sc.hasNextInt()){
System.out.println(sc.nextInt());
}
scanner.close();
理解scanner:
Scanner scanner = new Scanner(System.in);
System.out.println("请输入");
String str=scanner.nextLine();//接受第一个数据,不输入就一直等
if (scanner.hasNextDouble()){//判断下一个输入是否为浮点数,你不输入依然一直等
System.out.println("小数为:"+scanner.nextDouble());
}
if (scanner.hasNextInt()){//同上
System.out.println("整数位:"+scanner.nextInt());
}
System.out.println(str);
scanner.close();