Day2.2
Scanner 对象
Scanner s = new Scanner(System.in);
通过next方式接收内容
if(s.hasNext()){//可以不用if
String str = s.next();
System.out.println("输出的内容为 "+str);
}
用完就需要关闭
scanner.close();
同理.hasNextLine()也是一样的,意思是判断是否还有输入
-
next()和nextLine()的区别:next()空格后的会省略,有效字符前的空格也会省略,nextLine()就不会省略
-
if语句,hasNext()还可以判断输入的数据
if(scanner.hasNextInt()){
i = scanner.nextInt();
System.out.println("你输入的整数是 "+i);
}else{
System.out.println("你输入的不是整数!");
} -

浙公网安备 33010602011771号