Scanner对象
Java给用户提供了一个工具类,可以获取用户的输入。
java.util.Scanner是Java5的新特征,我们可以通过Scanner类来获取用户的输入。
基本语法
1 Scanner scanner = new Scanner(System.in);
通过Scanner类的 next() 与 nextLine() 方法获取输入的字符串
在读取前,我们可以使用 hasNext() 与 hasNextLine() 判断是否还有输入的数据。
1 //用hasNextLine()判断用户有没有输入字符串 2 if (scanner.hasNextLine()){ 3 //使用nextLine方式接收 4 String str = scanner.nextLine();//程序会等待用户输入完毕 5 System.out.println("输出的内容是:"+str); 6 } 7 //凡是属于IO流的类如果不关闭,会一直占用资源,要养成好习惯,用完就关掉 8 scanner.close();
-
一定要读取到有效字符后才可以结束输入
-
空白(空格)是next()的结束符(有效字符之前的空格,会被next()自动去掉)
-
next()不能得到带有空格的字符串
1 Scanner scanner = new Scanner(System.in); 2 System.out.println("使用next方式接收:"); 3 4 //用hasNext()判断用户有没有输入字符串 5 if (scanner.hasNext()){ 6 //使用next方式接收 7 String str = scanner.next();//程序会等待用户输入完毕 8 System.out.println("输出的内容是:"+str); 9 }

nextLine()
-
以Enter作为结束符,也就是说nextLine()方法返回的是输入回车之前的所有字符
-
nextLine()可以获得带有空格的字符串
1 package scanner; 2 3 import java.util.Scanner; 4 5 public class Demo03 { 6 public static void main(String[] args) { 7 Scanner scanner = new Scanner(System.in); 8 9 System.out.println("使用nextLine方式接收:"); 10 11 String str = scanner.nextLine(); 12 13 System.out.println("输出的内容为:"+str); 14 15 scanner.close(); 16 } 17 }

nextInt()、nextFloat()......
1 package scanner; 2 3 import java.util.Scanner; 4 5 public class Demo04 { 6 public static void main(String[] args) { 7 8 Scanner scanner = new Scanner(System.in); 9 10 int i = 0; 11 float f = 0.0f; 12 13 System.out.println("请输入整数:"); 14 15 if (scanner.hasNextInt()){ 16 i = scanner.nextInt(); 17 System.out.println("整数数据:"+i); 18 }else { 19 System.out.println("输入的不是整数数据!"); 20 } 21 22 System.out.println("请输入小数:"); 23 24 if (scanner.hasNextFloat()){ 25 f = scanner.nextFloat(); 26 System.out.println("小数数据:"+f); 27 }else { 28 System.out.println("输入的不是小数数据!"); 29 } 30 } 31 }


注:hasNext()和next()
-
sc,hasNext()和sc.next()都可以用来输入
但是hasNext()返回的是boolean类型,而next()返回的是你输入的那个值
可以这样理解:
-
sc.hasNext()接收了我们输入的值,存入sc中,并判断是否符合要求,再返回相应的布尔值
-
而sc.next()则是从sc中取出值,取完值后这个值就不存在了,若sc中没有值了,它也会要求输入一个值
1 Scanner sc = new Scanner(System.in); 2 if (sc.hasNext()){ 3 String str = sc.next(); 4 System.out.println("输出的内容为:"+str); 5 }
解析上面这段代码:
首先,sc.hasNext()就让我们输入一个值,若符合要求,则存入sc中,再返回true
然后,由于sc中有值,sc.next()从sc中取出(返回)这个值,并赋值给str。
1 Scanner sc = new Scanner(System.in); 2 3 String str = sc.next(); 4 5 System.out.println("输出的内容为:"+str);
解析上面这段代码:
由于sc中没有值,则sc.next()会要求我们输入一个值,并在这里取出(返回)值把值赋值给了str。
小练习
1 package scanner; 2 3 import java.util.Scanner; 4 5 public class Demo05 { 6 public static void main(String[] args) { 7 //我们可以输入多个数字,并求其总和和平均数,每输入一个数字用回车确认,通过输入非数字来结束输入并输出执行结果: 8 Scanner scanner = new Scanner(System.in); 9 10 double sum = 0;//和 11 int m = 0;//计算输入了多少个数字 12 13 //通过循环判断是否还有输入,并在里面对每一次进行求和统计 14 while (scanner.hasNextDouble()){ 15 double x = scanner.nextDouble(); 16 sum = sum + x; 17 m++; 18 System.out.println("你输入了第"+m+"个数据,然后当前结果是sum="+sum); 19 } 20 21 System.out.println(m + "个数字的和为" + sum); 22 System.out.println(m + "个数的平均值是" + (sum/m)); 23 24 scanner.close(); 25 } 26 }

浙公网安备 33010602011771号